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 (!@pnr.blank?)
@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!
model.update_attributes(hsh)is effectively the same as callingmodel.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’sfalse. You should have anif [...]check around this in the same way as you do for yoursavecall in thenewbranch of your cose