Reputation: 3225
I'm working on a controller action and, what I want to do is to modify a record in the data base.
I have the following code:
def save_reserve
@pnr = Pnr.find_by_email(params[:pnr][:email])
if ([email protected]?)
@pnr.update_attributes(params[:pnr])
else
@pnr = Pnr.new(params[:pnr])
if @pnr.save
...
else
...
end
end
end
Why @pnr.update_attributes(params[:pnr])
doesn't work?
However, if I do:
@pnr.update_attribute(:name, params[:pnr][:name])
@pnr.update_attribute(:lastname, params[:pnr][:lastname])
@pnr.update_attribute(:phone, params[:pnr][:phone])
@pnr.update_attribute(:addr, params[:pnr][:addr])
it works... Am I missing something?
Thank you!
Upvotes: 0
Views: 3999
Reputation: 138042
model.update_attributes(hsh)
is effectively the same as calling model.attributes = hsh; model.save
- it's subject to any callbacks and validations on the object.
model.update_attribute(field, value)
directly updates that field in the database without any callbacks or validation being run.
If you check the return value of @pnr.update_attributes(params[:pnr])
you'll probably see it's false
. You should have an if [...]
check around this in the same way as you do for your save
call in the new
branch of your cose
Upvotes: 3