# How Do I Fix Collection\_select?

**URL:** <https://www.ruby-forum.com/t/how-do-i-fix-collection-select/85345>\
**Category:** Rails\
**Created:** [February 12, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/how-do-i-fix-collection-select/85345 "2007-02-12T21:51:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [February 12, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/how-do-i-fix-collection-select/85345/1 "2007-02-12T21:51:11Z")

</div>

The bug described here([http://dev.rubyonrails.org/ticket/1125](http://dev.rubyonrails.org/ticket/1125)) is biting  
me hard at the moment, contrary to that ticket’s assertion that the bug  
has been squashed.

For those without time to read the ticket, collection\_select does not  
support the :selected method, apparently because it uses  
to\_collection\_select\_tag instead of to\_select\_tag. Moreover, because of  
this or some other deficiency, collection\_select does not automatically  
select the existing value when I move from my “new” view to my “edit”  
view.

if self.can\_fix\_in\_view\_code?  
question = “How?”  
puts question  
elsif !self.can\_fix\_in\_view\_code? and self.can\_override\_method?  
question = “How do I override to\_collection\_select\_tag?”  
puts question  
else  
question = “What DO I do to fix collection\_select?”  
puts question  
end

Thanks in advance,

-Matthew

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [February 12, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/how-do-i-fix-collection-select/85345/2 "2007-02-12T22:57:21Z")

</div>

collection\_select does display the correct defaults when editing a  
record. Double check your syntax, here is an example:

\<%= form.collection\_select :id, MyModel.find(:all), :id, :name %\>

If the value of :id on the left matches one of :id options on the  
right that will be the default value.

For the cases where you need to include a :selected parameter one easy  
solution is to just use select instead of collection\_select. It only  
adds a couple of extra parameters and :selected option will work as  
advertised. Another possibility would be to override the initialize  
method in your model class to set the default value. Then when a new  
object is passed into collection\_select it will already have the  
default and you don’t even need to use :selected.

Aaron

On Feb 12, 1:51 pm, Matthew F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [February 12, 2007, 11:23pm UTC](https://www.ruby-forum.com/t/how-do-i-fix-collection-select/85345/3 "2007-02-12T23:23:08Z")

</div>

Aaron wrote:

> collection\_select does display the correct defaults when editing a  
> record…
> 
> \<%= form.collection\_select :id, MyModel.find(:all), :id, :name %\>
> 
> If the value of :id on the left matches one of :id options on the  
> right that will be the default value…

My code is essentially:

\<% form\_for :contact, @contact do |f| -%\>  
\<%= f.collection\_select :type\_code, Lookup\_contact\_type.find(:all),  
:description, :description, {:include\_blank =\> true} %\>  
\<% end -%\>

type\_code is a method in my Contact model, and Lookup\_contact\_type is  
the model for its lookup table.

Is there a problem going cross-model like this? It works perfectly for  
entering the data–just chokes on edit.

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [February 12, 2007, 11:44pm UTC](https://www.ruby-forum.com/t/how-do-i-fix-collection-select/85345/4 "2007-02-12T23:44:26Z")

</div>

> \<% form\_for :contact, @contact do |f| -%\>  
> \<%= f.collection\_select :type\_code, Lookup\_contact\_type.find(:all),  
> :description, :description, {:include\_blank =\> true} %\>  
> \<% end -%\>

So does the value of @contact.type\_code for the record you are editing  
match a description from one of the entries in  
Lookup\_contact\_type.find(:all)? If so then that will be the default  
selection. If you are not sure what the values are try adding some  
debug calls to your view:

\<%= debug @contact %\>  
\<%= debug Lookup\_contact\_type.find(:all) %\>

Aaron

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [February 13, 2007, 12:49am UTC](https://www.ruby-forum.com/t/how-do-i-fix-collection-select/85345/5 "2007-02-13T00:49:31Z")

</div>

> So does the value of @contact.type\_code for the record you are editing  
> match a description from one of the entries in  
> Lookup\_contact\_type.find(:all)?..
> 
> \<%= debug @contact %\>  
> \<%= debug Lookup\_contact\_type.find(:all) %\>

Thank you so very much for the debug code.

It turns out my schema was set to char on the field in question, which  
was appending whitespace to the end of my lookup values as they were  
entered into the contacts table.

Fixed all that and collection\_select works like a charm.

Much obliged,

-Matthew
