From a240e526a8b4c5948a1c2a8ec49b6e2d1ad6ad23 Mon Sep 17 00:00:00 2001 From: Aaron Pfeifer Date: Tue, 26 Feb 2013 09:35:03 -0500 Subject: Fix ActiveRecord locking column defaults not getting persisted When partial inserts are enabled, overridden db defaults are ignored. This results in locking columns having a nil value for new records if the db default is null. This happens because the list of changed attributes for new records is always assumed to be empty. Solution: When a new record's default attributes are set, also initialize the list of changed attributes by comparing current values against what's stored as the column defaults in the database. --- activerecord/test/cases/locking_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index 0c896beb1d..77891b9156 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -193,11 +193,19 @@ class OptimisticLockingTest < ActiveRecord::TestCase def test_lock_without_default_sets_version_to_zero t1 = LockWithoutDefault.new assert_equal 0, t1.lock_version + + t1.save + t1 = LockWithoutDefault.find(t1.id) + assert_equal 0, t1.lock_version end def test_lock_with_custom_column_without_default_sets_version_to_zero t1 = LockWithCustomColumnWithoutDefault.new assert_equal 0, t1.custom_lock_version + + t1.save + t1 = LockWithCustomColumnWithoutDefault.find(t1.id) + assert_equal 0, t1.custom_lock_version end def test_readonly_attributes -- cgit v1.2.3