Forum: Ruby on Rails Insert to associated table question

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Sean C. (Guest)
on 2006-01-06 14:47
Hello Friendly Gurus,
I have a volunteers table and an hours tables.  I've edited the
respective models to have: "hours belongs_to :volunteer" and "volunteers
has_many :hours".  My problem is, I don't know how to set the
volunteer_id column in the hours table to the appropriate value so that
hours will belong to volunteers.

I've added the following line to list.rhtml in the volunteers view so I
can pass the volunteer id to the hours new action:
"<td><%= link_to 'Add Hours', :controller => 'hours', :action => 'new',
:id => volunteer %></td> "

My thought is that I would pass the volunteer id to the hours controller
and convert it into a hidden field in the "new" form so that we can use
it to populate the volunteer_id column.   This is how the PHP Spaghetti
coder in me would have done it, but I suppose there is a more elegant
Rails way to do this.  I hope you can help.

Thank you in advance!
-Sean C., Rails Nube and recovering PHP Spaghetti Coder
Sean C. (Guest)
on 2006-01-06 22:23
STFW, Sean! (Awesome!  I got to finally do that to someone that won't be
offended!) - parts 4 and 5
begin to show how to tie 2 tables together.
This topic is locked and can not be replied to.