So, I’ve got this action in order to save a model:
def create
logger.debug(params[:user_id])
group_id = params['approver']['group_id']
@approver = Approver.new(params[:approver])
@approver.user_id = params[:user_id]
respond_to do |format|
if @approver.save
logger.warn("Approver saved!")
flash[:notice] = "New approver has been added!"
format.html { redirect_to(group_path(group_id)) }
else
flash[:notice] = "Sorry .. had issues adding the approvers!"
format.html { redirect_to(group_path(group_id)) }
end
end
end
Parameters being passed in are:
Parameters: {"commit"=>"Submit", "authenticity_token"=>"C35lovRRjJzekruZiwTZjaMs4KgwiEJnXn10b0nD+0w=", "utf8"=>"✓", "user_id"=>["18"], "approver"=>{"group_id"=>"13"}}
And looking at my logs, the debug message in the action prints ’13’ as the correct value. However, the value being inserted into the database is always ‘1’ and here’s the snippet from the logs:
INSERT INTO `approvers` (`user_id`, `updated_at`, `created_at`, `group_id`) VALUES (1, '2011-07-13 04:58:51', '2011-07-13 04:58:51', 13)
To further complicate matters, in order to debug, if I change line 5 of the action to:
@approver.user_id = 19
it all works fine.
Can anyone explain what I’m doing wrong?
The
group_idisn’t your problem, that is going through fine. Your problem is this:So
params[:user_id]is actually an array but you’re treating it like an ID. Then, someone inside Rails is converting that array to 1. When you say this:Everything works because you’re assigning a Fixnum to
user_idand that’s whatuser_idexpects.You need to figure out why you’re getting an array
user_idand then fix that. Or, figure out what you should do with an array, maybe@approver.user_id = params[:user_id][0]makes sense, maybe not.