From 040b42cc4fa502acc5ce7492dcbee4fc6089004f Mon Sep 17 00:00:00 2001 From: Mike Breen Date: Sun, 9 Aug 2009 11:10:59 -0400 Subject: [PATCH] Serialized attributes should only be saved with partial_updates when the serialized attribute is present. --- activerecord/lib/active_record/dirty.rb | 2 +- activerecord/test/cases/dirty_test.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/dirty.rb b/activerecord/lib/active_record/dirty.rb index 4a2510a..f189651 100644 --- a/activerecord/lib/active_record/dirty.rb +++ b/activerecord/lib/active_record/dirty.rb @@ -143,7 +143,7 @@ module ActiveRecord if partial_updates? # Serialized attributes should always be written in case they've been # changed in place. - update_without_dirty(changed | self.class.serialized_attributes.keys) + update_without_dirty(changed | (attributes.keys & self.class.serialized_attributes.keys)) else update_without_dirty end diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index ac95bac..fb284af 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -287,6 +287,16 @@ class DirtyTest < ActiveRecord::TestCase assert_equal "b", topic.content[:b] end end + + def test_save_should_not_save_serialized_attribute_with_partial_updates_if_not_present + with_partial_updates(Topic) do + Topic.create!(:author_name => 'Bill', :content => {:a => "a"}) + topic = Topic.first(:select => 'id, author_name') + topic.update_attribute :author_name, 'John' + topic = Topic.first + assert_not_nil topic.content + end + end private def with_partial_updates(klass, on = true) -- 1.6.3