Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/8784323 to your computer and use it in GitHub Desktop.
Save JoshCheek/8784323 to your computer and use it in GitHub Desktop.
Nested Rollbacks not working (guessing its a sqlite thing)
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
@JoshCheek
Copy link
Author

Apparently the solution is to use requires_new: true, IDK why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment