From 39cb1bedb82814d7bf9de6834f88c31e4df27278 Mon Sep 17 00:00:00 2001 From: Kirill Lashuk Date: Sat, 28 Jan 2012 05:10:13 +0300 Subject: [PATCH] Do not serialize nil in serialized attribute. --- .../lib/active_record/coders/yaml_column.rb | 2 +- activerecord/test/cases/base_test.rb | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/coders/yaml_column.rb b/activerecord/lib/active_record/coders/yaml_column.rb index fb59d9fb07..77af540c3e 100644 --- a/activerecord/lib/active_record/coders/yaml_column.rb +++ b/activerecord/lib/active_record/coders/yaml_column.rb @@ -15,7 +15,7 @@ module ActiveRecord end def dump(obj) - YAML.dump obj + YAML.dump(obj) unless obj.nil? end def load(yaml) diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 6c3e4fc6d0..d70525b57d 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1301,11 +1301,24 @@ class BasicsTest < ActiveRecord::TestCase 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)