# Collection\_select not selecting item, unless

**URL:** <https://www.ruby-forum.com/t/collection-select-not-selecting-item-unless/109926>\
**Category:** Rails\
**Created:** [July 20, 2007, 10:01am UTC](https://www.ruby-forum.com/t/collection-select-not-selecting-item-unless/109926 "2007-07-20T10:01:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![atraeyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@atraeyu](https://www.ruby-forum.com/u/atraeyu)\
**Post date:** [July 20, 2007, 10:01am UTC](https://www.ruby-forum.com/t/collection-select-not-selecting-item-unless/109926/1 "2007-07-20T10:01:11Z")

</div>

I’m fairly new to Rails, so this could just be my stupidity rather than  
Rails.

My app has two simple models: record (id, category\_id, …) and  
category (id, name). In my generated scaffold \_form.html for the  
record model, I have the following collection\_select:

\<%= collection\_select ‘record’, ‘category’, @categories, :id, :name %\>

The option tag is properly populated, but the current category for the  
record being edited is not selected. However, if I change the above  
line to:

\<%= collection\_select ‘record’, ‘category\_id’, @categories, :id, :name  
%\>

Then it will select the proper category in the HTML form. But, when I  
try to save, it fails with an exception (ActiveRecord::RecordNotFound  
in RecordsController#update =\> “Couldn’t find Category without an  
ID”).

Here’s the edit and update methods from RecordsController:

def edit  
user = User.find(session[:user\_id])  
@record = user.records.find(params[:id])  
@categories = user.categories.find(:all, :order =\> ‘name’)  
end

def update  
user = User.find(session[:user\_id])  
@record = user.records.find(params[:id])  
# Replace the category id with the actual Category.  
params[:record][:category] =  
user.categories.find(params[:record][:category])  
if @record.update\_attributes(params[:record])  
flash[:notice] = ‘Record was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @record  
else  
render :action =\> ‘edit’  
end  
end

Any ideas what I’m doing wrong?

Thanks

n

P.S. You can browse all of my marvelous code here:  
[http://www.bluemarsh.com/hg/topsecret/](http://www.bluemarsh.com/hg/topsecret/) =\> click on “manifest” and  
you’ll see the typical Rails app structure.

---

<div class="post-metadata">

**Author:** ![atraeyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@atraeyu](https://www.ruby-forum.com/u/atraeyu)\
**Post date:** [July 20, 2007, 10:28am UTC](https://www.ruby-forum.com/t/collection-select-not-selecting-item-unless/109926/2 "2007-07-20T10:28:10Z")

</div>

If you change your collection select to “category\_id”, you have to  
change it in your controller too ofcourse, did you do that ?  
so params[:record][:category] should be params[:record][:category\_id]

Stijn

---

<div class="post-metadata">

**Author:** ![atraeyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@atraeyu](https://www.ruby-forum.com/u/atraeyu)\
**Post date:** [July 20, 2007, 5:12pm UTC](https://www.ruby-forum.com/t/collection-select-not-selecting-item-unless/109926/3 "2007-07-20T17:12:31Z")

</div>

Good point, that would address the error. Thanks.

However, the question remains: why do I have to change this to be  
different than every single example of collection\_select I have found  
so far, including the AWDWR book, postings on this list, and the Rails  
wiki itself?

Thanks

n
