Forum: Ruby on Rails using select() to relate tables

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.
3dda2c76123692d982b648851f710269?d=identicon&s=25 Bealach Na Bo (Guest)
on 2006-04-06 19:34
(Received via mailing list)
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

I've done this by adding <%= select('', '', venue.find_all.collect
{|venue|}) %>
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.

3bef3fc4d0cbcf58f3d66c106e2fd16f?d=identicon&s=25 Mark Van Holstyn (Guest)
on 2006-04-06 19:46
(Received via mailing list)
You will most likelt want something like this...

<%= select("pc", "venue_id", Venue.find_all.collect {|v| [,]}

this will create a select with the name as the displayed value, and put
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
Venue described by @pc.venue_id. Then you can reuse this for for editing
PC as well.

3dda2c76123692d982b648851f710269?d=identicon&s=25 Bealach Na Bo (Guest)
on 2006-04-06 19:55
(Received via mailing list)
Brilliant Mark! It worked like a charm. Now I've got to do my homework
find out how to abstract as much as possible into a controller or a

This topic is locked and can not be replied to.