Ticket #7293: add_nil_support_to_serialize.diff
| File add_nil_support_to_serialize.diff, 1.8 kB (added by sandofsky, 2 years ago) |
|---|
-
test/base_test.rb
old new 1118 1118 1119 1119 def test_serialized_attribute_with_class_constraint 1120 1120 myobj = MyObject.new('value1', 'value2') 1121 topic = Topic.create("content" => myobj) 1121 topic = Topic.new 1122 assert_nil topic.content 1123 1124 topic.content = myobj 1125 assert topic.save 1122 1126 Topic.serialize(:content, Hash) 1123 1127 1124 1128 assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content } -
lib/active_record/base.rb
old new 574 574 575 575 # Specifies that the attribute by the name of +attr_name+ should be serialized before saving to the database and unserialized 576 576 # after loading from the database. The serialization is done through YAML. If +class_name+ is specified, the serialized 577 # object must be of that class on retrieval or+SerializationTypeMismatch+ will be raised.577 # object must be of that class on retrieval, or nil. Otherwise, +SerializationTypeMismatch+ will be raised. 578 578 def serialize(attr_name, class_name = Object) 579 579 serialized_attributes[attr_name.to_s] = class_name 580 580 end … … 1958 1958 def unserialize_attribute(attr_name) 1959 1959 unserialized_object = object_from_yaml(@attributes[attr_name]) 1960 1960 1961 if unserialized_object.is_a?(self.class.serialized_attributes[attr_name]) 1961 if unserialized_object.is_a?(self.class.serialized_attributes[attr_name]) || unserialized_object.nil? 1962 1962 @attributes[attr_name] = unserialized_object 1963 1963 else 1964 1964 raise SerializationTypeMismatch,