diff options
5 files changed, 22 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index 895c16b9b3..ab86d8bad1 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -54,7 +54,7 @@ module ActiveRecord # Define read method for serialized attribute. def define_read_method_for_serialized_attribute(attr_name) - access_code = "@attributes_cache['#{attr_name}'] ||= unserialize_attribute('#{attr_name}')" + access_code = "@attributes_cache['#{attr_name}'] ||= @attributes['#{attr_name}']" generated_attribute_methods.module_eval("def _#{attr_name}; #{access_code}; end; alias #{attr_name} _#{attr_name}", __FILE__, __LINE__) end diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb index 5eba94ec66..6a593a7e0e 100644 --- a/activerecord/lib/active_record/attribute_methods/write.rb +++ b/activerecord/lib/active_record/attribute_methods/write.rb @@ -23,8 +23,7 @@ module ActiveRecord if (column = column_for_attribute(attr_name)) && column.number? @attributes[attr_name] = convert_number_column_value(value) else - coder = self.class.serialized_attributes[attr_name] - @attributes[attr_name] = coder ? coder.dump(value) : value + @attributes[attr_name] = value end end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 6b82b827b0..04b479bdb2 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1446,6 +1446,12 @@ MSG # post.title # => 'hello world' def init_with(coder) @attributes = coder['attributes'] + + (@attributes.keys & self.class.serialized_attributes.keys).each do |key| + coder = self.class.serialized_attributes[key] + @attributes[key] = coder.load @attributes[key] + end + @attributes_cache, @previously_changed, @changed_attributes = {}, {}, {} @association_cache = {} @aggregation_cache = {} @@ -1748,8 +1754,8 @@ MSG if include_readonly_attributes || (!include_readonly_attributes && !self.class.readonly_attributes.include?(name)) - value = if klass.serialized_attributes[name] - @attributes[name] + value = if coder = klass.serialized_attributes[name] + coder.dump @attributes[name] else # FIXME: we need @attributes to be used consistently. # If the values stored in @attributes were already type diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 76110b73e4..a96796f9ff 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -15,6 +15,12 @@ module ActiveRecord def validate_each(record, attribute, value) finder_class = find_finder_class_for(record) + coder = record.class.serialized_attributes[attribute.to_s] + + if value && coder + value = coder.dump value + end + sql, params = mount_sql_and_params(finder_class, record.class.quoted_table_name, attribute, value) relation = finder_class.unscoped.where(sql, *params) diff --git a/activerecord/test/cases/serialization_test.rb b/activerecord/test/cases/serialization_test.rb index 25dbcc9fc2..677d659f39 100644 --- a/activerecord/test/cases/serialization_test.rb +++ b/activerecord/test/cases/serialization_test.rb @@ -23,6 +23,12 @@ class SerializationTest < ActiveRecord::TestCase @contact = Contact.new(@contact_attributes) end + def test_serialized_init_with + topic = Topic.allocate + topic.init_with('attributes' => { 'content' => '--- foo' }) + assert_equal 'foo', topic.content + end + def test_to_xml xml = REXML::Document.new(topics(:first).to_xml(:indent => 0)) bonus_time_in_current_timezone = topics(:first).bonus_time.xmlschema |