From 75ffd8701d04d943eddcafee08e19b90bce8936c Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 7 Feb 2012 23:23:18 +0000 Subject: Fix attribute_before_type_cast for serialized attributes. Fixes #4837. --- activerecord/lib/active_record/attribute_methods/serialization.rb | 8 ++++++++ activerecord/lib/active_record/core.rb | 2 ++ 2 files changed, 10 insertions(+) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb index 0c8e4e4b9a..7efef73472 100644 --- a/activerecord/lib/active_record/attribute_methods/serialization.rb +++ b/activerecord/lib/active_record/attribute_methods/serialization.rb @@ -88,6 +88,14 @@ module ActiveRecord super end end + + def read_attribute_before_type_cast(attr_name) + if serialized_attributes.include?(attr_name) + super.unserialized_value + else + super + end + end end end end diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index a2ce620354..e87fba550b 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -209,6 +209,8 @@ module ActiveRecord # The dup method does not preserve the timestamps (created|updated)_(at|on). def initialize_dup(other) cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast) + self.class.initialize_attributes(cloned_attributes) + cloned_attributes.delete(self.class.primary_key) @attributes = cloned_attributes -- cgit v1.2.3