Skip callbacks in Rails when saving record
This is valid for at least Rails 6. It is also dependent on low-level implementation, but should be rather safe to use.
def without_update_callbacks(record)
existing_caller = record.method(:_run_update_callbacks)
record.define_singleton_method(:_run_update_callbacks, ->(&block){ block.call })
yield
ensure
record.define_singleton_method(:_run_update_callbacks, existing_caller)
end
Usage
user = User.last
user.name = "Tony"
user.save # runs callbacks
user.name = "Stark"
without_update_callbacks(user) do
user.save # does not run callbacks
end
Tweet