# Initializing collection\_select

**URL:** <https://www.ruby-forum.com/t/initializing-collection-select/164906>\
**Category:** Rails\
**Created:** [April 7, 2009, 9:33am UTC](https://www.ruby-forum.com/t/initializing-collection-select/164906 "2009-04-07T09:33:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LukeG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f19dbf/32.png) [@LukeG](https://www.ruby-forum.com/u/LukeG)\
**Post date:** [April 7, 2009, 9:33am UTC](https://www.ruby-forum.com/t/initializing-collection-select/164906/1 "2009-04-07T09:33:51Z")

</div>

I’m a noob. I’ve trolled the web and groups for help, but I just don’t  
know enough Rails to apply the solutions to my particular problem. I  
even got the screencast from Pragmatic Programmers, but still no dice.

I’m having a problem getting my collection\_select to initialize with a  
previously stored value. I have items and categories, with many items  
to one category. When I edit an item, the category list populates  
correctly, but I cannot figure out how to set the default to whatever  
category was already stored for that item.

Running Rails 2.3.2, here is the code:

_controller:_  
def edit  
@observation = Observation.find(params[:id])  
end

_view:_  
\<%= f.label “Category” %\>  
  
\<%=  
collection\_select(:observation\_category\_id,  
@category\_id,  
ObservationCategory.find(:all),  
:id,  
:name,  
{:selected =\> @observation.observation\_category\_id})  
%\>

_models:_  
class Observation \< ActiveRecord::Base  
attr\_accessible :name, :icon\_url, :observation\_catategory\_id  
belongs\_to :observation\_category  
end

class ObservationCategory \< ActiveRecord::Base  
has\_many :observations  
end

_html:_  
Category  
  
exercise

finance nutrition mood energy focus sleep junk\_cat satiation

---

<div class="post-metadata">

**Author:** ![LukeG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f19dbf/32.png) [@LukeG](https://www.ruby-forum.com/u/LukeG)\
**Post date:** [April 7, 2009, 9:44am UTC](https://www.ruby-forum.com/t/initializing-collection-select/164906/2 "2009-04-07T09:44:25Z")

</div>

On Apr 7, 5:21 am, LukeG [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m a noob. I’ve trolled the web and groups for help, but I just don’t  
> know enough Rails to apply the solutions to my particular problem. I  
> even got the screencast from Pragmatic Programmers, but still no dice.
> 
> I’m having a problem getting my collection\_select to initialize with a  
> previously stored value. I have items and categories, with many items  
> to one category. When I edit an item, the category list populates  
> correctly, but I cannot figure out how to set the default to whatever  
> category was already stored for that item.

collection\_select is like the various model helpers in that you do  
collection\_select ‘instance\_variable\_name’, ‘method\_name’, …  
ie in this instance collection\_select ‘observation’,  
‘category\_id’, …

Seeing as how you’ve already got a form\_for setup, you should be able  
to do f.collection\_select ‘category\_id’, …  
Calling it on the form builder means you don’t need to tell rails  
which object you are working with.

Fred

---

<div class="post-metadata">

**Author:** ![LukeG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f19dbf/32.png) [@LukeG](https://www.ruby-forum.com/u/LukeG)\
**Post date:** [April 8, 2009, 6:57am UTC](https://www.ruby-forum.com/t/initializing-collection-select/164906/3 "2009-04-08T06:57:19Z")

</div>

ive come across this problem and as far as my searching took me,  
collection\_select does not support the :selected option.  
you can handle this by initializing the “selected” value in the  
controller for the appropriate attribute before rendering the partial.

The code you have shown above is slightly flawed in the syntax of  
collection\_select as Fred has already pointed out. I think you want

\<%= f.collection\_select :observation\_category\_id,  
ObservationCategory.find(:all), :id, :name %\>

So in your case, the problem really was the syntax. The above should  
work for your edit form ie. the correct option will be selected based  
on the existing value (built into collection\_select).

The lack of support for the :selected option in collection\_select  
however, is a side issue. Anyone know if there’s a fix for it? Besides  
initializing the value for it in the controller before rendering the  
partial?

On Apr 7, 12:43 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![LukeG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f19dbf/32.png) [@LukeG](https://www.ruby-forum.com/u/LukeG)\
**Post date:** [May 31, 2009, 9:48am UTC](https://www.ruby-forum.com/t/initializing-collection-select/164906/4 "2009-05-31T09:48:45Z")

</div>

hey… thanx man…  
i was wondering why my code wasn’t working…  
well my problem was similar to yours, but a little bit different…  
but you gave me a concrete solution to my problem through this  
thread…  
thanx…  
good luck 🙂

---

<div class="post-metadata">

**Author:** ![LukeG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f19dbf/32.png) [@LukeG](https://www.ruby-forum.com/u/LukeG)\
**Post date:** [April 8, 2009, 6:21pm UTC](https://www.ruby-forum.com/t/initializing-collection-select/164906/5 "2009-04-08T18:21:17Z")

</div>

Thanks for the replies. The code, as y’all noted, was incorrect. This  
fixed it:

```
collection_select(:observation,
         :observation_category_id,
        ObservationCategory.find(:all),
        :id,
        :name)

```

I read somewhere that if the :method parameter returns a value in  
the :value parameter, that value will default to the selected item in  
the list, and this turned out to be true, thus eliminating the need  
for :selected.

If I understand your points, the f.collection\_select syntax is  
preferred over what I have. I’ll make that update, and thanks for the  
advice.

-Luke
