Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Do not serialize nil in serialized attribute.

  • Loading branch information...
commit 39cb1bedb82814d7bf9de6834f88c31e4df27278 1 parent d6e41f3
@KL-7 KL-7 authored
View
2  activerecord/lib/active_record/coders/yaml_column.rb
@@ -15,7 +15,7 @@ def initialize(object_class = Object)
end
def dump(obj)
- YAML.dump obj
+ YAML.dump(obj) unless obj.nil?
end
def load(yaml)
View
15 activerecord/test/cases/base_test.rb
@@ -1301,11 +1301,24 @@ def test_serialized_string_attribute
assert_equal(myobj, topic.content)
end
- def test_nil_serialized_attribute_with_class_constraint
+ def test_nil_serialized_attribute_without_class_constraint
topic = Topic.new
assert_nil topic.content
end
+ def test_nil_not_serialized_without_class_constraint
+ assert Topic.new(:content => nil).save
+ assert_equal 1, Topic.where(:content => nil).count
+ end
+
+ def test_nil_not_serialized_with_class_constraint
+ Topic.serialize :content, Hash
+ assert Topic.new(:content => nil).save
+ assert_equal 1, Topic.where(:content => nil).count
+ ensure
+ Topic.serialize(:content)
+ end
+
def test_should_raise_exception_on_serialized_attribute_with_type_mismatch
myobj = MyObject.new('value1', 'value2')
topic = Topic.new(:content => myobj)
Please sign in to comment.
Something went wrong with that request. Please try again.