# Using select() to relate tables

**URL:** <https://www.ruby-forum.com/t/using-select-to-relate-tables/57041>\
**Category:** Rails\
**Created:** [April 6, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/using-select-to-relate-tables/57041 "2006-04-06T19:34:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bealach\_Na\_Bo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e19b73/32.png) [@Bealach\_Na\_Bo](https://www.ruby-forum.com/u/Bealach_Na_Bo)\
**Post date:** [April 6, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/using-select-to-relate-tables/57041/1 "2006-04-06T19:34:11Z")

</div>

I’m sorry to be asking trivial questions, but I’m new to RoR and am  
hooked :), so I’m very impatient to get things up and running.

given two related tables, “pcs” and “venues” (venues have many PC’s and  
each PC belongs to one venue only) I can use a select() to present venue  
names in the “new PC” form along with other PC details to be  
registered.

I’ve done this by adding \<%= select(’’, ‘’, venue.find\_all.collect  
{|venue|  
venue.name}) %\>  
to my \_form.rhtml for registering new PC’s.

Now when you register a new PC, you can choose the venue that it  
belongs to by name, but I need to then send the venue\_id (the foreign  
key in pcs) and not the venue name from the form. Can someone advise  
me on how I might do this?

Also, I know that I’m really supposed to write something in the  
appropriate controller or write a helper method to populate a variable  
which I can then use in my \_form.rhtml, but I’m not sure how to do  
that and would appreciate pointers there too.

Bealach

---

<div class="post-metadata">

**Author:** ![Bealach\_Na\_Bo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e19b73/32.png) [@Bealach\_Na\_Bo](https://www.ruby-forum.com/u/Bealach_Na_Bo)\
**Post date:** [April 6, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/using-select-to-relate-tables/57041/2 "2006-04-06T19:46:26Z")

</div>

You will most likelt want something like this…

\<%= select(“pc”, “venue\_id”, Venue.find\_all.collect {|v| [v.name, v.id]}  
)  
%\>

this will create a select with the name as the displayed value, and put  
the  
id as the actual value submitted with the form. also by adding pc and  
venue\_id as the first 2 parameters, the select will automatically select  
the  
Venue described by @pc.venue\_id. Then you can reuse this for for editing  
a  
PC as well.

mark

---

<div class="post-metadata">

**Author:** ![Bealach\_Na\_Bo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e19b73/32.png) [@Bealach\_Na\_Bo](https://www.ruby-forum.com/u/Bealach_Na_Bo)\
**Post date:** [April 6, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/using-select-to-relate-tables/57041/3 "2006-04-06T19:55:46Z")

</div>

Brilliant Mark! It worked like a charm. Now I’ve got to do my homework  
and  
find out how to abstract as much as possible into a controller or a  
helper.  
Thanks.

Bealach
