Manual ActiveRecord Transactions via ActiveRecord::Base.connection.transaction_manager
Especially useful in the rails console
I wouldn't recommend using this in application code because having automatic handling of transactions via blocks with ActiveRecord::Base.transaction is way safer. However there are times where I want to open a transaction but have my ruby code eval'd in realtime in the rails console. Here's the code snippet to accomplish this using the familiar begin
, commit
, and rollback
commands:
transaction_manager = ActiveRecord::Base.connection.transaction_manager
transaction_manager.begin_transaction
transaction_manager.commit_transaction
transaction_manager.rollback_transaction
Ruby Forest Druid.