# Collection\_select

**URL:** <https://www.ruby-forum.com/t/collection-select/135610>\
**Category:** Rails\
**Created:** [April 23, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/collection-select/135610 "2008-04-23T16:12:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [April 23, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/collection-select/135610/1 "2008-04-23T16:12:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [April 23, 2008, 4:32pm UTC](https://www.ruby-forum.com/t/collection-select/135610/2 "2008-04-23T16:32:22Z")

</div>

## 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](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [April 23, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/collection-select/135610/3 "2008-04-23T16:43:55Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [April 23, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/collection-select/135610/4 "2008-04-23T17:39:32Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [April 23, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/collection-select/135610/5 "2008-04-23T17:21:42Z")

</div>

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](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [April 23, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/collection-select/135610/6 "2008-04-23T17:56:18Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![erik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5ac83/32.png) [@erik](https://www.ruby-forum.com/u/erik)\
**Post date:** [April 23, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/collection-select/135610/7 "2008-04-23T17:46:35Z")

</div>

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
