First off I''m new to Ruby and to Rails so be gentle :)
I''m cutting my teeth on a Timesheet app to replace a paper system.
It''s
real simple, Jobs, Companies, Employees, and Timesheets.
So Employees belong to Companies, that part was easy. But now I''m
trying
to make many Jobs belong to Companies which requires a join table. Here
are my tables.
mysql> describe companies ;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(10) | | PRI | NULL | auto_increment |
| name | varchar(50) | | UNI | | |
| street1 | varchar(70) | | | | |
| street2 | varchar(70) | | | | |
| city | varchar(70) | | | | |
| state | char(2) | | | | |
| zip | varchar(10) | | | | |
+---------+-------------+------+-----+---------+----------------+
mysql> describe companies_jobs;
+------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| company_id | int(11) | | | 0 | |
| job_id | int(11) | | | 0 | |
+------------+---------+------+-----+---------+-------+
mysql> describe employees;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | | PRI | NULL | auto_increment |
| company_id | int(10) | | | 0 | |
| name | varchar(50) | | MUL | | |
| street1 | varchar(70) | | | | |
| street2 | varchar(70) | | | | |
| city | varchar(70) | | | | |
| state | char(2) | | | | |
| zip | varchar(10) | | | | |
+------------+------------------+------+-----+---------+----------------+
So I want a dropdown menu of available companies on the job add/edit
pages. I added this line to my Jobs class:
belongs_to :company
and this one to Company
has_many :jobs
which didn''t work at all, so I tried has_and_belongs_to_many in both
classes based on an example on the from the Rails site. That seems to
break too, I get a strange error. Which brings me to question #1, coming
from PHP I would love a print_r() equivalent in ruby. It would help me
figure out what was going on while I get used to the language. At least
a way to see the sql that is executing would be sufficient.
Oh and on my new/edit .rhtml pages I''m using this code to draw the
company dropdown:
<select name="job[company_id]">
<% @companies.each do |company| %>
<option value="<%= company.id %>" <%= ''
selected'' if company.id ==
@job.company.id %>>
<%= company.name %>
</option>
<% end %>
I''m sure the solution is simple, and I truly have searched for an
answer
before bothering the list.
Any help would be greatly appreciated, and I apologize for the size of
this post.
Thanks in advance.
--
* * * * * * * *
*BILL EVANS*
developer
*JUICY TEMPLES*
2424 E. Robinson St.
Orlando, Florida 32803
407 895 5015 phone
407 895 1883 fax
www.juicytemples.com
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
On Mon, Feb 28, 2005 at 09:31:16AM -0500, Bill Evans wrote:> First off I''m new to Ruby and to Rails so be gentle :)Hey, me too!> I''m cutting my teeth on a Timesheet app to replace a paper system. It''s > real simple, Jobs, Companies, Employees, and Timesheets. > > So Employees belong to Companies, that part was easy. But now I''m trying > to make many Jobs belong to Companies which requires a join table. Here > are my tables. > > mysql> describe companies ; > +---------+-------------+------+-----+---------+----------------+ > | Field | Type | Null | Key | Default | Extra | > +---------+-------------+------+-----+---------+----------------+ > | id | int(10) | | PRI | NULL | auto_increment | > | name | varchar(50) | | UNI | | | > | street1 | varchar(70) | | | | | > | street2 | varchar(70) | | | | | > | city | varchar(70) | | | | | > | state | char(2) | | | | | > | zip | varchar(10) | | | | | > +---------+-------------+------+-----+---------+----------------+ > > mysql> describe companies_jobs; > +------------+---------+------+-----+---------+-------+ > | Field | Type | Null | Key | Default | Extra | > +------------+---------+------+-----+---------+-------+ > | company_id | int(11) | | | 0 | | > | job_id | int(11) | | | 0 | | > +------------+---------+------+-----+---------+-------+ > > mysql> describe employees; > +------------+------------------+------+-----+---------+----------------+ > | Field | Type | Null | Key | Default | Extra | > +------------+------------------+------+-----+---------+----------------+ > | id | int(10) unsigned | | PRI | NULL | auto_increment | > | company_id | int(10) | | | 0 | | > | name | varchar(50) | | MUL | | | > | street1 | varchar(70) | | | | | > | street2 | varchar(70) | | | | | > | city | varchar(70) | | | | | > | state | char(2) | | | | | > | zip | varchar(10) | | | | | > +------------+------------------+------+-----+---------+----------------+ > > So I want a dropdown menu of available companies on the job add/edit > pages. I added this line to my Jobs class: > > belongs_to :company > > and this one to Company > > has_many :jobs > > which didn''t work at all, so I tried has_and_belongs_to_many in both > classes based on an example on the from the Rails site. That seems to > break too, I get a strange error. Which brings me to question #1, coming > from PHP I would love a print_r() equivalent in ruby. It would help me > figure out what was going on while I get used to the language. At least > a way to see the sql that is executing would be sufficient. > > Oh and on my new/edit .rhtml pages I''m using this code to draw the > company dropdown: > > <select name="job[company_id]"> > <% @companies.each do |company| %> > <option value="<%= company.id %>" <%= '' selected'' if company.id == > @job.company.id %>> > <%= company.name %> > </option> > <% end %>Have you populated @companies in your job controller? This doesn''t happen automatically from mapping the relation. Try something like: @companies = Company.find_all For the equivalent of print_r, you want .inspect (i.e.: @companies.inspect).> I''m sure the solution is simple, and I truly have searched for an answer > before bothering the list. > > Any help would be greatly appreciated, and I apologize for the size of > this post. > > Thanks in advance. > > > -- > > > > * * * * * * * * > > *BILL EVANS* > developer > > *JUICY TEMPLES* > 2424 E. Robinson St. > Orlando, Florida 32803 > 407 895 5015 phone > 407 895 1883 fax > www.juicytemples.com >> _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- - Adam ----- ** My new project --> http://www.visiognomy.com/daily ** Flagship blog --> http://www.aquick.org/blog Hire me: [ http://www.adamfields.com/Adam_Fields_Resume.htm ] Links: [ http://del.icio.us/fields ] Photos: [ http://www.aquick.org/photoblog ]
Adam Fields <rails23049809@...> writes:> Have you populated <at> companies in your job controller? >This doesn''t > happen automatically from mapping the relation.Yea, the dropdown is populated properly. It''s the saving that isn''t working.> For the equivalent of print_r, you want .inspect >(i.e.: <at> companies.inspect).Thanks for this it will help alot! Thanks for your help.
On Mon, Feb 28, 2005 at 03:52:18PM +0000, Bill Evans wrote:> Adam Fields <rails23049809@...> writes: > > > Have you populated <at> companies in your job controller? > >This doesn''t > > happen automatically from mapping the relation. > > Yea, the dropdown is populated properly. > It''s the saving that isn''t working.What''s the error you''re getting? -- - Adam ----- ** My new project --> http://www.visiognomy.com/daily ** Flagship blog --> http://www.aquick.org/blog Hire me: [ http://www.adamfields.com/Adam_Fields_Resume.htm ] Links: [ http://del.icio.us/fields ] Photos: [ http://www.aquick.org/photoblog ]