Skip to content

Instantly share code, notes, and snippets.

@lulalala
Last active January 22, 2018 08:45
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 lulalala/42adf084be279df2912530a4bc4fdc6d to your computer and use it in GitHub Desktop.
Save lulalala/42adf084be279df2912530a4bc4fdc6d to your computer and use it in GitHub Desktop.
Rails rollback does not revert to previous dirty state bug
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :foos do |t|
t.integer :n
end
create_table :bars do |t|
t.integer :n
end
end
class Foo < ApplicationRecord
end
class Bar < ApplicationRecord
validates :n, presence: true, numericality: { greater_than_or_equal_to: 0 }
end
class BugTest < Minitest::Test
def test_not_so_dirty
foo = Foo.create(n:1)
bar = Bar.create(n:1)
begin
Bar.transaction do
foo.n = -1
foo.save!
bar.n= -1
bar.save! # will trigger validation error
end
rescue ActiveRecord::RecordInvalid => invalid
end
assert_equal 1, foo.n_was
assert_equal 1, bar.n_was
assert_equal -1, foo.n
assert_equal -1, bar.n
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment