Hi, it would be great if someone could help me.
I’m using an after_update callback in a particular model to keep a
history of some object’s modifications. It works find but I have a
little inconvenient.
For some specific reasons, in the controller Ineed to do first an
object.update_attributes and then an object.save! and, as the
after_update callback it’s called after both instructions, for each time
I save an object, two modifications are recorded in the history.
One way to bypass this problem is to forget the callbacks and implement
something by myself but I don’t want to do that, at least not yet.
Hi, it would be great if someone could help me.
I’m using an after_update callback in a particular model to keep a
history of some object’s modifications. It works find but I have a
little inconvenient.
For some specific reasons, in the controller Ineed to do first an
object.update_attributes and then an object.save! and, as the
after_update callback it’s called after both instructions, for each time
I save an object, two modifications are recorded in the history.
You don’t need to call save at all: update_attributes automatically
saves the record.
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
Hi, it would be great if someone could help me.
I’m using an after_update callback in a particular model to keep a
history of some object’s modifications. It works find but I have a
little inconvenient.
For some specific reasons, in the controller Ineed to do first an
object.update_attributes and then an object.save! and, as the
after_update callback it’s called after both instructions, for each time
I save an object, two modifications are recorded in the history.
You don’t need to call save at all: update_attributes automatically
saves the record.
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.