from http://wiki.rubyonrails.com/rails/pages/CheckboxHABTM
When I create a new item and try setting the foo_ids where foo is a many-to-many, it doesnâÂÂt get saved to the database. When I update a record that already exists in the database, it works (the join records are created). To get it to work, I need to re-fetch the newly create row by its id, then call update_attributes on it.
{{{ def create
@department = Department.new(params[:department])
# FIX! can't set many-to-many relationships on creation
if @department.save
Department.find(@department.id).update_attributes(params[:department]) # hack to get around many-to-many not saving until object has an id
flash[:notice] = 'Department was successfully created.'
redirect_to :action => 'list'
else
render :action => 'new'
end
end
}}}
Judging from the discussion on the wiki, this is a bug which appeared in 1.0.0, as this used to work in older versions of rails.