123456789_123456789_123456789_123456789_123456789_

Module: ActiveRecord::Transactions

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Defined in: activerecord/lib/active_record/transactions.rb

Overview

See ClassMethods for documentation.

Class Method Summary

Instance Method Summary

DSL Calls

included

[ GitHub ]


18
19
20
21
22
23
24
25
# File 'activerecord/lib/active_record/transactions.rb', line 18

included do
  define_callbacks :commit, :rollback,
                   terminator: ->(_, result) { result == false },
                   scope: [:kind, :name]

  mattr_accessor :raise_in_transactional_callbacks, instance_writer: false
  self.raise_in_transactional_callbacks = false
end

Instance Method Details

#add_to_transaction

Add the record to the current transaction so that the after_rollback and after_commit callbacks can be called.

[ GitHub ]

  
# File 'activerecord/lib/active_record/transactions.rb', line 330

def add_to_transaction
  if has_transactional_callbacks?
    self.class.connection.add_transaction_record(self)
  else
    sync_with_transaction_state
    set_transaction_state(self.class.connection.transaction_state)
  end
  remember_transaction_record_state
end

#rollback_active_record_state!

Reset id and @new_record if the transaction rolls back.

[ GitHub ]

  
# File 'activerecord/lib/active_record/transactions.rb', line 299

def rollback_active_record_state!
  remember_transaction_record_state
  yield
rescue Exception
  restore_transaction_record_state
  raise
ensure
  clear_transaction_record_state
end

#transaction(options = {}, &block)

See Transactions::ClassMethods for detailed documentation.

[ GitHub ]

  
# File 'activerecord/lib/active_record/transactions.rb', line 276

def transaction(options = {}, &block)
  self.class.transaction(options, &block)
end

#with_transaction_returning_status

Executes method within a transaction and captures its return value as a status flag. If the status is true the transaction is committed, otherwise a ROLLBACK is issued. In any case the status flag is returned.

This method is available within the context of an Base instance.

[ GitHub ]

  
# File 'activerecord/lib/active_record/transactions.rb', line 346

def with_transaction_returning_status
  status = nil
  self.class.transaction do
    add_to_transaction
    begin
      status = yield
    rescue ActiveRecord::Rollback
      clear_transaction_record_state
      status = nil
    end

    raise ActiveRecord::Rollback unless status
  end
  status
ensure
  if @transaction_state && @transaction_state.committed?
    clear_transaction_record_state
  end
end