Module: ActiveRecord::Transactions

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_record/transactions.rb

Overview

See ActiveRecord::Transactions::ClassMethods for documentation.

Defined Under Namespace

Modules: ClassMethods Classes: TransactionError

Instance Method Summary collapse

Instance Method Details

#add_to_transactionObject

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



276
277
278
279
280
# File 'lib/active_record/transactions.rb', line 276

def add_to_transaction
  if self.class.connection.add_transaction_record(self)
    remember_transaction_record_state
  end
end

#committed!Object

Call the after_commit callbacks



260
261
262
263
264
# File 'lib/active_record/transactions.rb', line 260

def committed! #:nodoc:
  _run_commit_callbacks
ensure
  clear_transaction_record_state
end

#destroyObject

:nodoc:



234
235
236
# File 'lib/active_record/transactions.rb', line 234

def destroy #:nodoc:
  with_transaction_returning_status { super }
end

#rollback_active_record_state!Object

Reset id and @new_record if the transaction rolls back.



249
250
251
252
253
254
255
256
257
# File 'lib/active_record/transactions.rb', line 249

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

#rolledback!(force_restore_state = false) ⇒ Object

Call the after rollback callbacks. The restore_state argument indicates if the record state should be rolled back to the beginning or just to the last savepoint.



268
269
270
271
272
# File 'lib/active_record/transactions.rb', line 268

def rolledback!(force_restore_state = false) #:nodoc:
  _run_rollback_callbacks
ensure
  restore_transaction_record_state(force_restore_state)
end

#saveObject

:nodoc:



238
239
240
241
242
# File 'lib/active_record/transactions.rb', line 238

def save(*) #:nodoc:
  rollback_active_record_state! do
    with_transaction_returning_status { super }
  end
end

#save!Object

:nodoc:



244
245
246
# File 'lib/active_record/transactions.rb', line 244

def save!(*) #:nodoc:
  with_transaction_returning_status { super }
end

#transaction(&block) ⇒ Object

See ActiveRecord::Transactions::ClassMethods for detailed documentation.



230
231
232
# File 'lib/active_record/transactions.rb', line 230

def transaction(&block)
  self.class.transaction(&block)
end

#with_transaction_returning_statusObject

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 ActiveRecord::Base instance.



288
289
290
291
292
293
294
295
296
# File 'lib/active_record/transactions.rb', line 288

def with_transaction_returning_status
  status = nil
  self.class.transaction do
    add_to_transaction
    status = yield
    raise ActiveRecord::Rollback unless status
  end
  status
end