diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-09-07 13:39:27 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-09-07 13:39:27 -0700 |
commit | c6015cbcd8b6ed716c0add2874a17a495250ef0a (patch) | |
tree | 006d88d1b93e70dc652827144d9cb2b67ebeb11a | |
parent | c8a2dd3caca94ce41f37442512b90343dcbc6161 (diff) | |
download | rails-c6015cbcd8b6ed716c0add2874a17a495250ef0a.tar.gz rails-c6015cbcd8b6ed716c0add2874a17a495250ef0a.tar.bz2 rails-c6015cbcd8b6ed716c0add2874a17a495250ef0a.zip |
serialized attributes should be serialized before validation [#5525 state:resolved]
-rw-r--r-- | activerecord/lib/active_record/base.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 6 |
3 files changed, 12 insertions, 3 deletions
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 |