module ActiveRecord::Transactions::ClassMethods
def after_commit(*args, &block)
Note that transactional fixtures do not play well with this feature. Please
after_commit :do_bar_baz, on: [:update, :destroy]
after_commit :do_foo_bar, on: [:create, :update]
after_commit :do_baz, on: :destroy
after_commit :do_bar, on: :update
after_commit :do_foo, on: :create
the +:on+ option:
You can specify that the callback should only be fired by a certain action with
This callback is called after a record has been created, updated, or destroyed.
def after_commit(*args, &block) set_options_for_callbacks!(args) set_callback(:commit, :after, *args, &block) unless ActiveRecord::Base.raise_in_transactional_callbacks ActiveSupport::Deprecation.warn(CALLBACK_WARN_MESSAGE) end end
def after_rollback(*args, &block)
This callback is called after a create, update, or destroy are rolled back.
def after_rollback(*args, &block) set_options_for_callbacks!(args) set_callback(:rollback, :after, *args, &block) unless ActiveRecord::Base.raise_in_transactional_callbacks ActiveSupport::Deprecation.warn(CALLBACK_WARN_MESSAGE) end end
def assert_valid_transaction_action(actions)
def assert_valid_transaction_action(actions) if (actions - ACTIONS).any? raise ArgumentError, ":on conditions for after_commit and after_rollback callbacks have to be one of #{ACTIONS}" end end
def set_options_for_callbacks!(args)
def set_options_for_callbacks!(args) options = args.last if options.is_a?(Hash) && options[:on] fire_on = Array(options[:on]) assert_valid_transaction_action(fire_on) options[:if] = Array(options[:if]) options[:if] << "transaction_include_any_action?(#{fire_on})" end end
def transaction(options = {}, &block)
def transaction(options = {}, &block) # See the ConnectionAdapters::DatabaseStatements#transaction API docs. connection.transaction(options, &block) end