Skip to content

Commit

Permalink
serialized attributes should be serialized before validation [#5525 s…
Browse files Browse the repository at this point in the history
…tate:resolved]
  • Loading branch information
tenderlove committed Sep 7, 2010
1 parent c8a2dd3 commit c6015cb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -1685,8 +1685,8 @@ def arel_attributes_values(include_primary_key = true, include_readonly_attribut
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
Expand Down
5 changes: 5 additions & 0 deletions activerecord/lib/active_record/validations/uniqueness.rb
Expand Up @@ -17,6 +17,11 @@ def validate_each(record, attribute, value)
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)
Expand Down
6 changes: 5 additions & 1 deletion activerecord/test/cases/base_test.rb
Expand Up @@ -909,9 +909,13 @@ def test_quoting_arrays
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

Expand Down

0 comments on commit c6015cb

Please sign in to comment.