Collection_select

I am attempting to populate a collection_select object with data from
a separate table than the one the collection will be displayed.

Specifically, I would like to add a new ‘project’, and in one of the
fields, I would like to have a drop down list which shows different
project types. Any advice on how to do this would be greatly
appreciated.

controller

#DDM = Drop Down Menu
#example your table project only contains column : ID & PROJECT_NAME

@project_list = Project.find_all

in VIEW

[1] SELECT_TAG
Code:
<%= select_tag “project”,“<option value="0">
Select Project”+
options_for_select(@project_list.map(&:project_name).sort.uniq) %>

[2] COLLECTION_SELECT

<%= collection_select(:project, :id, @project_list, :id, :project_name,
options ={:prompt => “-Select Project”}, :class =>“project”) %>

Reinhart
http://teapoci.blogspot.com

Thank you for the quick response.
However, I must not have been clear in my question:

Assume that I have two tables: project, type

PROJECT contains columns: ID PROJECT_NAME TYPE_ID
TYPE contains: ID TYPE_NAME

When I create a new project, I would like to have a #DDM, populated by
the TYPE_NAME field.

On Apr 23, 10:32 pm, Visit Indonesia 2008 <rails-mailing-l…@andreas-

Thanks, that is a very tricky solution. I was starting from an empty
project table, with a populated type table. I will include some dummy
projects to feed this list. Am I correct in assuming that this is the
necessary solution to this? It seems that querying the large product
table will be more expensive than simply populating off of the smaller
type table.

On Apr 23, 11:21 pm, Visit Indonesia 2008 <rails-mailing-l…@andreas-

CONTROLLER :

@type_list = Project.find(:all, :include => {:types=>{} }).collect{|x|
x.type.type_name}.sort.uniq

in VIEW :

<%= select_tag “type_name”,“<option value="0">
Select Project Type”+
options_for_select(@type_list) %>

OR

<%= select_tag “type_name”, options_for_select(@type_list,
@type_list[0]) %>

Cheer,

Reinhart
http://teapoci.blogspot.com

Perfect. I have been struggling with that all day. It worked like
magic.
Thanks for the help.

-Erik

On Apr 23, 11:45 pm, Frederick C. [email protected]

On 23 Apr 2008, at 16:38, erik wrote:

Thanks, that is a very tricky solution. I was starting from an empty
project table, with a populated type table. I will include some dummy
projects to feed this list. Am I correct in assuming that this is the
necessary solution to this? It seems that querying the large product
table will be more expensive than simply populating off of the smaller
type table.

If you’ve got the types in a table, then

collection_select ‘project’, ‘type’, Type.find(:all), :type_name, :id

(assuming you’re editing the instance variable @person). Before you do
any of this, choose a name that isn’t type: that’s used by rails for
single table inheritance

Fred