I published an article on disabling ActiveModel callbacks, but I’m not completely sure this is the prettiest way to do something like this.
Mongoid::Timestamps
adds a before save callback that updates the updated_at
field. Let's say I don't want that in some cases and I disable the callback like this:
class User
# I'm using Mongoid, but this should work for anything based on
# ActiveModel.
include Mongoid::Document
include Mongoid::Timestamps
def sneaky_update(attributes)
User.skip_callback(:save, :before, :set_updated_at)
User.update_attributes(attributes)
User.set_callback(:save, :before, :set_updated_at)
end
end
Is calling skip_callback
followed by set_callback
to set the removed callback again a bad idea? How would you do this? :)