module ActiveRecord::Transactions::ClassMethods
def after_commit(*args, &block)
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, prepend_option) set_callback(:commit, :after, *args, &block) end
def after_create_commit(*args, &block)
def after_create_commit(*args, &block) set_options_for_callbacks!(args, on: :create, **prepend_option) set_callback(:commit, :after, *args, &block) end
def after_destroy_commit(*args, &block)
def after_destroy_commit(*args, &block) set_options_for_callbacks!(args, on: :destroy, **prepend_option) set_callback(:commit, :after, *args, &block) 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, prepend_option) set_callback(:rollback, :after, *args, &block) end
def after_save_commit(*args, &block)
def after_save_commit(*args, &block) set_options_for_callbacks!(args, on: [ :create, :update ], **prepend_option) set_callback(:commit, :after, *args, &block) end
def after_update_commit(*args, &block)
def after_update_commit(*args, &block) set_options_for_callbacks!(args, on: :update, **prepend_option) set_callback(:commit, :after, *args, &block) 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 before_commit(*args, &block) # :nodoc:
def before_commit(*args, &block) # :nodoc: set_options_for_callbacks!(args) set_callback(:before_commit, :before, *args, &block) end
def current_transaction
To check if a transaction was opened, use current_transaction.open?.
An object is always returned, whether or not a transaction is currently active.
which can be a top level transaction, a savepoint, or the absence of a transaction.
Returns a representation of the current transaction state,
def current_transaction connection_pool.active_connection&.current_transaction&.user_transaction || Transaction::NULL_TRANSACTION end
def prepend_option
def prepend_option if ActiveRecord.run_after_transaction_callbacks_in_order_defined { prepend: true } else {} end end
def set_callback(name, *filter_list, &block)
Similar to ActiveSupport::Callbacks::ClassMethods#set_callback, but with
def set_callback(name, *filter_list, &block) options = filter_list.extract_options! filter_list << options if name.in?([:commit, :rollback]) && options[:on] fire_on = Array(options[:on]) assert_valid_transaction_action(fire_on) options[:if] = [ -> { transaction_include_any_action?(fire_on) }, *options[:if] ] end super(name, *filter_list, &block) end
def set_options_for_callbacks!(args, enforced_options = {})
def set_options_for_callbacks!(args, enforced_options = {}) options = args.extract_options!.merge!(enforced_options) args << options if options[:on] fire_on = Array(options[:on]) assert_valid_transaction_action(fire_on) options[:if] = [ -> { transaction_include_any_action?(fire_on) }, *options[:if] ] end end
def transaction(**options, &block)
def transaction(**options, &block) with_connection do |connection| connection.transaction(**options, &block) end end