From d09b2fd09dbd8f74d1d2909c7e931fb0a664b315 Mon Sep 17 00:00:00 2001 From: kennyj Date: Wed, 11 Apr 2012 02:48:28 +0900 Subject: Fix #5797. Error calling dup method on AR model with serialized field --- activerecord/lib/active_record/attribute_methods/serialization.rb | 7 ++++--- activerecord/lib/active_record/core.rb | 2 +- activerecord/lib/active_record/locking/optimistic.rb | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb index 165785c8fb..706fbf0546 100644 --- a/activerecord/lib/active_record/attribute_methods/serialization.rb +++ b/activerecord/lib/active_record/attribute_methods/serialization.rb @@ -72,12 +72,13 @@ module ActiveRecord self.serialized_attributes = serialized_attributes.merge(attr_name.to_s => coder) end - def initialize_attributes(attributes) #:nodoc: - super + def initialize_attributes(attributes, options = {}) #:nodoc: + serialized = (options.delete(:serialized) { true }) ? :serialized : :unserialized + super(attributes, options) serialized_attributes.each do |key, coder| if attributes.key?(key) - attributes[key] = Attribute.new(coder, attributes[key], :serialized) + attributes[key] = Attribute.new(coder, attributes[key], serialized) end end diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 80c6f20b1a..1fa6c701bb 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -241,7 +241,7 @@ module ActiveRecord ## def initialize_dup(other) # :nodoc: cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast) - self.class.initialize_attributes(cloned_attributes) + self.class.initialize_attributes(cloned_attributes, :serialized => false) cloned_attributes.delete(self.class.primary_key) diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index a3412582fa..05e052b953 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -168,7 +168,7 @@ module ActiveRecord # start the lock version at zero. Note we can't use # locking_enabled? at this point as # @attributes may not have been initialized yet. - def initialize_attributes(attributes) #:nodoc: + def initialize_attributes(attributes, options = {}) #:nodoc: if attributes.key?(locking_column) && lock_optimistically attributes[locking_column] ||= 0 end -- cgit v1.2.3