1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

serialized attributes should be serialized before validation [#5525 state:resolved]

This commit is contained in:
Aaron Patterson 2010-09-07 13:39:27 -07:00
parent c8a2dd3cac
commit c6015cbcd8
3 changed files with 12 additions and 3 deletions

View file

@ -1685,8 +1685,8 @@ MSG
if include_readonly_attributes || (!include_readonly_attributes && !self.class.readonly_attributes.include?(name)) if include_readonly_attributes || (!include_readonly_attributes && !self.class.readonly_attributes.include?(name))
value = read_attribute(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)) if value && self.class.serialized_attributes.key?(name)
value = value.to_yaml value = YAML.dump value
end end
attrs[self.class.arel_table[name]] = value attrs[self.class.arel_table[name]] = value
end end

View file

@ -17,6 +17,11 @@ module ActiveRecord
table = finder_class.unscoped table = finder_class.unscoped
table_name = record.class.quoted_table_name 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) sql, params = mount_sql_and_params(finder_class, table_name, attribute, value)
relation = table.where(sql, *params) relation = table.where(sql, *params)

View file

@ -909,9 +909,13 @@ class BasicsTest < ActiveRecord::TestCase
MyObject = Struct.new :attribute1, :attribute2 MyObject = Struct.new :attribute1, :attribute2
def test_serialized_attribute def test_serialized_attribute
Topic.serialize("content", MyObject)
myobj = MyObject.new('value1', 'value2') myobj = MyObject.new('value1', 'value2')
topic = Topic.create("content" => myobj) topic = Topic.create("content" => myobj)
Topic.serialize("content", MyObject) assert_equal(myobj, topic.content)
topic.reload
assert_equal(myobj, topic.content) assert_equal(myobj, topic.content)
end end