active record transaction
Post on 17-Feb-2017
291 Views
Preview:
TRANSCRIPT
ActiveRecord Transaction Shiva Bhusal
- Transactions are protective blocks
- Makes sense if some operations are interdependent.
- Transactions enforce the integrity of the database
- Guard the data against program errors or database break-downs
Introduction
- Basically you should use transaction blocks whenever you have a number of statements that must be executed together or not at all.
- The classic example is a transfer between two accounts where you can only have a deposit if the withdrawal succeeded and vice versa
Use Cases
- save and destroy are automatically wrapped in a transaction
Alternative way
- Exceptions thrown within a transaction block will be propagated (after
triggering the ROLLBACK)
- ActiveRecord::Rollback exception, triggers a ROLLBACK when raised- not be re-raised by the transaction block.
Transaction Rollback Triggers
- There are two types of callbacks associated with committing and rolling
back transactions:
- after_commit
- after_rollback
Callbacks
Thanks
References
http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.htmlhttp://markdaggett.com/blog/2011/12/01/transactions-in-rails/https://cbabhusal.wordpress.com/2015/12/15/
activerecord-callbacks-and-the-order-in-which-they-get-executed/
top related