Tutorial by Examples

For example: ActiveRecord::Base.transaction do david.withdrawal(100) mary.deposit(100) end This example will only take money from David and give it to Mary if neither withdrawal nor deposit raise an exception. Exceptions will force a ROLLBACK that returns the database to the state before ...
Though the transaction class method is called on some ActiveRecord class, the objects within the transaction block need not all be instances of that class. This is because transactions are per-database connection, not per-model. In this example a balance record is transactionally saved even though ...
A transaction acts on a single database connection. If you have multiple class-specific databases, the transaction will not protect interaction among them. One workaround is to begin a transaction on each class whose models you alter: Student.transaction do Course.transaction do course.enro...
Both #save and #destroy come wrapped in a transaction that ensures that whatever you do in validations or callbacks will happen under its protected cover. So you can use validations to check for values that the transaction depends on or you can raise exceptions in the callbacks to rollback, includin...
There are two types of callbacks associated with committing and rolling back transactions: after_commit and after_rollback. after_commit callbacks are called on every record saved or destroyed within a transaction immediately after the transaction is committed. after_rollback callbacks are called o...
ActiveRecord::Base.transaction uses the ActiveRecord::Rollback exception to distinguish a deliberate rollback from other exceptional situations. Normally, raising an exception will cause the .transaction method to rollback the database transaction and pass on the exception. But if you raise an Activ...

Page 1 of 1