collection_select not selecting item, unless...

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/ => click on "manifest" and you'll see the typical Rails app structure.

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