active record transaction
TRANSCRIPT
![Page 1: Active record transaction](https://reader036.vdocuments.mx/reader036/viewer/2022091121/58a708fa1a28ab4c438b6137/html5/thumbnails/1.jpg)
ActiveRecord Transaction Shiva Bhusal
![Page 2: Active record transaction](https://reader036.vdocuments.mx/reader036/viewer/2022091121/58a708fa1a28ab4c438b6137/html5/thumbnails/2.jpg)
- 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
![Page 3: Active record transaction](https://reader036.vdocuments.mx/reader036/viewer/2022091121/58a708fa1a28ab4c438b6137/html5/thumbnails/3.jpg)
- 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
![Page 4: Active record transaction](https://reader036.vdocuments.mx/reader036/viewer/2022091121/58a708fa1a28ab4c438b6137/html5/thumbnails/4.jpg)
- save and destroy are automatically wrapped in a transaction
Alternative way
![Page 5: Active record transaction](https://reader036.vdocuments.mx/reader036/viewer/2022091121/58a708fa1a28ab4c438b6137/html5/thumbnails/5.jpg)
- 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
![Page 6: Active record transaction](https://reader036.vdocuments.mx/reader036/viewer/2022091121/58a708fa1a28ab4c438b6137/html5/thumbnails/6.jpg)
- There are two types of callbacks associated with committing and rolling
back transactions:
- after_commit
- after_rollback
Callbacks
![Page 7: Active record transaction](https://reader036.vdocuments.mx/reader036/viewer/2022091121/58a708fa1a28ab4c438b6137/html5/thumbnails/7.jpg)
Thanks
![Page 8: Active record transaction](https://reader036.vdocuments.mx/reader036/viewer/2022091121/58a708fa1a28ab4c438b6137/html5/thumbnails/8.jpg)
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/