Active record transaction

8
ActiveRecord Transaction Shiva Bhusal

Transcript of Active record transaction

Page 1: Active record transaction

ActiveRecord Transaction Shiva Bhusal

Page 2: Active record transaction

- 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

- 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

- save and destroy are automatically wrapped in a transaction

Alternative way

Page 5: Active record transaction

- 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

- There are two types of callbacks associated with committing and rolling

back transactions:

- after_commit

- after_rollback

Callbacks

Page 7: Active record transaction

Thanks

Page 8: Active record transaction

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/