See Nested Transactions for more information.
TL;DR if you're going to use nested transactions you probably should not raise ActiveRecord::Rollback
See Nested Transactions for more information.
TL;DR if you're going to use nested transactions you probably should not raise ActiveRecord::Rollback
| def test_user(step) | |
| AdminUser.where(email: "sad.trombone#{step}@rails.org") | |
| end | |
| [ActiveRecord::Rollback, RuntimeError].each do |my_error| | |
| puts "Testing with #{my_error.to_s}" | |
| puts 'Deleting test users if they exists' | |
| test_user(1).delete_all | |
| test_user(2).delete_all | |
| puts 'sad.trombone 1 exists? ' + test_user(1).exists?.to_s | |
| puts 'sad.trombone 2 exists? ' + test_user(2).exists?.to_s | |
| puts 'About to enter transaction block' | |
| begin | |
| AdminUser.transaction do | |
| AdminUser.create!(email: '[email protected]', password: 'password') | |
| AdminUser.transaction do | |
| AdminUser.create!(email: '[email protected]', password: 'password') | |
| raise my_error | |
| end | |
| end | |
| rescue | |
| end | |
| puts 'Done with transactions' | |
| puts 'sad.trombone 1 exists? ' + test_user(1).exists?.to_s | |
| puts 'sad.trombone 2 exists? ' + test_user(2).exists?.to_s | |
| end |
| Testing with ActiveRecord::Rollback | |
| Deleting test users if they exists | |
| sad.trombone 1 exists? false | |
| sad.trombone 2 exists? false | |
| About to enter transaction block | |
| Done with transactions | |
| sad.trombone 1 exists? true | |
| sad.trombone 2 exists? true | |
| Testing with RuntimeError | |
| Deleting test users if they exists | |
| sad.trombone 1 exists? false | |
| sad.trombone 2 exists? false | |
| About to enter transaction block | |
| Done with transactions | |
| sad.trombone 1 exists? false | |
| sad.trombone 2 exists? false |