# Collection\_select question

**URL:** <https://www.ruby-forum.com/t/collection-select-question/95714>\
**Category:** Rails\
**Created:** [June 10, 2006, 10:10am UTC](https://www.ruby-forum.com/t/collection-select-question/95714 "2006-06-10T10:10:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [June 10, 2006, 10:10am UTC](https://www.ruby-forum.com/t/collection-select-question/95714/1 "2006-06-10T10:10:25Z")

</div>

How do I specify which option should be initially selected when using  
the collection\_select command? I’ve tried:

collection\_select(:selectionfilter, :producer\_id, Producer.find(:all),  
:id, :name, {:include\_blank =\> true, :selected\_value =\>  
@initial\_selection})

However it doesn’t return the desired results (the option with the value  
of @initial\_selection selected).

I’ve also tried:

collection\_select(:selectionfilter, :producer\_id, Producer.find(:all),  
:id, :name, {:include\_blank =\> true}, {:selected\_value =\>  
@initial\_selection})

which likewise doesn’t work. I’ve verified that @initial\_selection is  
returning the correct value.

The docs say that collection\_select uses the  
options\_for\_collection\_select, which has :selected\_value as one of its  
options, so it seems the above should work, but I’m obviously missing  
something.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [June 10, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/collection-select-question/95714/2 "2006-06-10T14:55:25Z")

</div>

On Sat, 2006-06-10 at 10:10 +0200, FranÃ§ois Montel wrote:

> I’ve also tried:  
> options, so it seems the above should work, but I’m obviously missing  
> something.

* * *

that’s not how I would use it. Assuming that ‘your model’ is related to  
producers, I would use it something more like this…

(controller code)  
@producer = Producer.find(:all)

(view code)  
\<%= options = [[‘Select’, ‘’]] + @producer.collect {  
|producer| [producer.name, producer.id] }  
select ‘YOUR\_MODEL’, ‘producer\_id’, options %\>

Craig

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [January 11, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/collection-select-question/95714/3 "2007-01-11T21:49:40Z")

</div>

Francois, did you figure out a way to do this?

Do you know how to set multiple options to be selected?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [June 10, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/collection-select-question/95714/4 "2006-06-10T18:24:10Z")

</div>

Craig W. wrote:

> (controller code)  
> @producer = Producer.find(:all)
> 
> (view code)  
> \<%= options = [[‘Select’, ‘’]] + @producer.collect {  
> |producer| [producer.name, producer.id] }  
> select ‘YOUR\_MODEL’, ‘producer\_id’, options %\>

Thanks, Craig, but your code above yields the same results that I was  
getting. (It seems to me, my original code is a shortcut for the above.)

The problem I’m having is not with the select box malfunctioning, but  
rather that the select box is not displaying the initial value that I  
want it to. It’s used to set a filter, and when the user selects a  
value, the page is refreshed, filtered on that value. At that point I  
would like the select box to display the last value selected (the value  
that is being filtered on), which I have stored in @initial\_selection.  
However, despite using the “:selected” option, the select box still show  
the default first value. That’s what I’m trying to solve.

In other words, my code is yielding this:

Bob Harry John

when I want it to yield this (assuming the user chose “Harry” last time  
around).

Bob Harry John

Any other clues? Thanks!
