diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index f0bf07e65b1afd1bf37b5d08cdf9bbd40c7e598b..90885b18c0ab792b08d6cd62d9d1d8eb32b26d2b 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Ensure optimistic locking handles nil #lock_version values properly. Closes #10510 [rick] + * Make the Fixtures Test::Unit enhancements more supporting for double-loaded test cases. Closes #10379 [brynary] * Fix that validates_acceptance_of still works for non-existent tables (useful for bootstrapping new databases). Closes #10474 [hasmanyjosh] diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index ecd6a2b258cb5ec671f64cc06793b6385ecf89ef..799309c17b1c7fdd746616d54dcae2b0cd1f21e9 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -70,7 +70,7 @@ def update_with_lock #:nodoc: return update_without_lock unless locking_enabled? lock_col = self.class.locking_column - previous_value = send(lock_col) + previous_value = send(lock_col).to_i send(lock_col + '=', previous_value + 1) begin diff --git a/activerecord/test/locking_test.rb b/activerecord/test/locking_test.rb index 97fbfaaeb18921f661669b5ec94bdb8acb522c09..9a3b76e810cefb43d94f307005232055bd865276 100644 --- a/activerecord/test/locking_test.rb +++ b/activerecord/test/locking_test.rb @@ -64,6 +64,15 @@ def test_lock_new assert_raises(ActiveRecord::StaleObjectError) { p2.save! } end + + def test_lock_new_with_nil + p1 = Person.new(:first_name => 'anika') + p1.save! + p1.lock_version = nil # simulate bad fixture or column with no default + p1.save! + assert_equal 1, p1.lock_version + end + def test_lock_column_name_existing t1 = LegacyThing.find(1)