Last active
August 29, 2015 13:55
-
-
Save JoshCheek/8784323 to your computer and use it in GitHub Desktop.
Nested Rollbacks not working (guessing its a sqlite thing)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record' | |
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:' | |
ActiveRecord::Schema.define do | |
self.verbose = false | |
create_table :users | |
end | |
User = Class.new ActiveRecord::Base | |
def create_user_then_raise_error_in_transaction | |
User.transaction do | |
User.create | |
raise | |
end | |
rescue | |
end | |
# a single transaction rolls it back | |
create_user_then_raise_error_in_transaction | |
User.count # => 0 | |
# nested transactions do not work | |
User.transaction { create_user_then_raise_error_in_transaction } | |
User.count # => 1 | |
# for reaaaaaalz >.< | |
create_user_then_raise_error_in_transaction | |
create_user_then_raise_error_in_transaction | |
create_user_then_raise_error_in_transaction | |
create_user_then_raise_error_in_transaction | |
create_user_then_raise_error_in_transaction | |
create_user_then_raise_error_in_transaction | |
User.count # => 1 | |
User.transaction { create_user_then_raise_error_in_transaction } | |
User.count # => 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apparently the solution is to use
requires_new: true
, IDK why.