# Insert to associated table question

**URL:** <https://www.ruby-forum.com/t/insert-to-associated-table-question/51732>\
**Category:** Rails\
**Created:** [January 6, 2006, 1:47pm UTC](https://www.ruby-forum.com/t/insert-to-associated-table-question/51732 "2006-01-06T13:47:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [January 6, 2006, 1:47pm UTC](https://www.ruby-forum.com/t/insert-to-associated-table-question/51732/1 "2006-01-06T13:47:47Z")

</div>

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:  
"

\<%= link\_to ‘Add Hours’, :controller =\> ‘hours’, :action =\> ‘new’,  
:id =\> volunteer %\> "

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

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [January 6, 2006, 9:23pm UTC](https://www.ruby-forum.com/t/insert-to-associated-table-question/51732/2 "2006-01-06T21:23:00Z")

</div>

STFW, Sean! (Awesome! I got to finally do that to someone that won’t be  
offended!)  
[Radar – O’Reilly](http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html) - parts 4 and 5  
begin to show how to tie 2 tables together.
