1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
Make sure we handle explicitly passed nil's to lock_version as well.
An explicitly passed nil value is now converted to 0 on LockingType,
so that we don't end up with ActiveRecord::StaleObjectError in update record
optimistic locking

Fixes #24695
This commit is contained in:
Vipul A M 2016-04-24 04:01:18 +05:30
parent c9c5788a52
commit 210729c4cc
2 changed files with 10 additions and 0 deletions

View file

@ -190,6 +190,10 @@ module ActiveRecord
super.to_i super.to_i
end end
def serialize(value)
super.to_i
end
def init_with(coder) def init_with(coder)
__setobj__(coder['subtype']) __setobj__(coder['subtype'])
end end

View file

@ -169,6 +169,12 @@ class OptimisticLockingTest < ActiveRecord::TestCase
assert_equal 1, p1.lock_version assert_equal 1, p1.lock_version
end end
def test_lock_new_when_explicitly_passing_nil
p1 = Person.new(:first_name => 'anika', lock_version: nil)
p1.save!
assert_equal 0, p1.lock_version
end
def test_touch_existing_lock def test_touch_existing_lock
p1 = Person.find(1) p1 = Person.find(1)
assert_equal 0, p1.lock_version assert_equal 0, p1.lock_version