123456789_123456789_123456789_123456789_123456789_

Module: ActiveRecord::Transactions

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Base, ActiveRecord::InternalMetadata, ActiveRecord::SchemaMigration
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 ]


10
11
12
13
14
15
16
17
# File 'activerecord/lib/active_record/transactions.rb', line 10

included do
  define_callbacks :commit, :rollback,
                   :before_commit,
                   :before_commit_without_transaction_enrollment,
                   :commit_without_transaction_enrollment,
                   :rollback_without_transaction_enrollment,
                   scope: [:kind, :name]
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 367

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 323

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 300

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 383

def with_transaction_returning_status
  status = nil
  self.class.transaction do
    add_to_transaction
    status = yield
    raise ActiveRecord::Rollback unless status
  end
  status
ensure
  if @transaction_state && @transaction_state.committed?
    clear_transaction_record_state
  end
end