You don't need to call save at all: update_attributes automatically saves the record.
Jeff softiesonrails.com
You don't need to call save at all: update_attributes automatically saves the record.
Jeff softiesonrails.com
Thanks Jeff. update_attribues(params[:my_object]) automatically saves the updated params in the previous view but there are modifications that doesn't come directly from the previous view. For instance, I keep track of
who did the last modification or release the database record once it is recorded.if my_object.update_attributes(params[:my:object]) # after_update is called my_object.last_editor = current_user my_object.locked = false my_object.save! # after_update is called again end
Any tip? Is there a way o keep all the modifications in memory and to save it all at the same time? Thanks
update_attributes just does attributes= and then saves so you could dp
my_object.last_editor = current_user my_object.locked = false my_object.update_attributes(params[:my:object])
or
my_object.attributes = params[:my:object] if my_object.valid? my_object.last_editor = current_user my_object.locked = false my_object.save! # after_update is called again end
if you're fussed about only setting locked/last_editor if the
attributes given were valid.
Fred
You can still use mass assignment, just change your logic a bit
@foo = Foo.find(params[:id]
@foo.attributes = params[:foo] # instead of update_attributes
if @foo.valid?
@my_object.last_editor = current_user @my_object.locked = false
save!
end
However, if it were me… I’d make a model method
(in your model)