Ruby on Rails - getting started with active record transactions- ruby on rails tutorial - rails guides - rails tutorial - ruby rails
getting started with active record transactions
Active Record Transactions can be applied to Model classes as well as Model instances, the objects within the transaction block need not all be instances of same class. This is because transactions are per-database connection, not per-model.
Example
Using save with a bang ensures that transaction will be automatically rolled back when the exception is thrown and after the rollback, control goes to the rescue block for the exception. Make sure you rescue the exceptions thrown from the save! in Transaction Block. If you don't want to use save!, you can manually raise raise ActiveRecord::Rollback when the save fails. You need not handle this exception. It will then rollback the transaction and take the control to the next statement after transaction block.