# Collection\_select not remembering selected value

**URL:** <https://www.ruby-forum.com/t/collection-select-not-remembering-selected-value/156738>\
**Category:** Rails\
**Created:** [January 2, 2009, 8:57am UTC](https://www.ruby-forum.com/t/collection-select-not-remembering-selected-value/156738 "2009-01-02T08:57:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [January 2, 2009, 8:57am UTC](https://www.ruby-forum.com/t/collection-select-not-remembering-selected-value/156738/1 "2009-01-02T08:57:22Z")

</div>

Hello-

I have a form that I’m using form\_for on to bind the form to my order  
object. Within the form, I want to select a product (each order has  
one product, which makes this easy). In my order and product models,  
an order belongs\_to a product. I didn’t specify that a product  
has\_many orders (although I did test that the problem still exists  
even if I do specify that). Here’s the code I’m using to generate the  
select list:

@packages = Package.find(:all, :order =\> ‘name’) #this is in my  
controller

\<%= f.collection\_select :product, @products, :id, :name, { :prompt =\>  
‘–select one–’} %\>

The call to collection\_select creates the list of options just fine,  
but if my order has a value set for product, it doesn’t get set as the  
selected value.

I’ve tried a slew of other variations on the call to collection\_select  
(e.g., using :product\_id instead of :product) and all seem to fail.

Does anyone know what I might be doing wrong?

Regards,  
Eric

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [January 2, 2009, 9:07am UTC](https://www.ruby-forum.com/t/collection-select-not-remembering-selected-value/156738/2 "2009-01-02T09:07:20Z")

</div>

One more thing. I debugged my app and when I inspect my @order object,  
the product field is being correctly set. I debugged a little deeper  
and it looks like at some point, Rails is trying to decide to set a  
particular option to selected by evaluating something like the  
following (assuming product id 7 was selected):

“7” == 7

It looks like, for the purpose of setting my model’s attributes, the  
product id is being turned into an integer, but for selecting a value  
in collection\_select, it is keeping it a string. Very confusing.

-Eric

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [January 2, 2009, 9:28am UTC](https://www.ruby-forum.com/t/collection-select-not-remembering-selected-value/156738/3 "2009-01-02T09:28:27Z")

</div>

Ah ha, I figured it out. In my migration, I had accidentally specified  
that the product\_id column should be a string, instead of an integer.  
Once I changed the type, everything worked great.

Regards,  
Eric
