diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 21453f5e9a..ef4834811c 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1685,8 +1685,8 @@ MSG if include_readonly_attributes || (!include_readonly_attributes && !self.class.readonly_attributes.include?(name)) value = read_attribute(name) - if value && ((self.class.serialized_attributes.has_key?(name) && (value.acts_like?(:date) || value.acts_like?(:time))) || value.is_a?(Hash) || value.is_a?(Array)) - value = value.to_yaml + if value && self.class.serialized_attributes.key?(name) + value = YAML.dump value end attrs[self.class.arel_table[name]] = value end diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index cc3d123cea..cb1d2ae421 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -17,6 +17,11 @@ module ActiveRecord table = finder_class.unscoped table_name = record.class.quoted_table_name + + if value && record.class.serialized_attributes.key?(attribute.to_s) + value = YAML.dump value + end + sql, params = mount_sql_and_params(finder_class, table_name, attribute, value) relation = table.where(sql, *params) diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 07b06e15a3..dcb1da7d91 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -909,9 +909,13 @@ class BasicsTest < ActiveRecord::TestCase MyObject = Struct.new :attribute1, :attribute2 def test_serialized_attribute + Topic.serialize("content", MyObject) + myobj = MyObject.new('value1', 'value2') topic = Topic.create("content" => myobj) - Topic.serialize("content", MyObject) + assert_equal(myobj, topic.content) + + topic.reload assert_equal(myobj, topic.content) end