我有一个序列化的专栏
class TestSerialize < ActiveRecord::Base
serialize :parameters, Array
end
当我尝试分配字符串
时a = TestSerialize.new
a.parameters = "inappropriate type"
按照预期我得到了
ActiveRecord::SerializationTypeMismatch:Attribute was supposed to be a Array, but was a String. -- "inappropriate type".
但是当我试图重新分配“参数”字段时,我仍然得到与之前指定值相同的错误
a.parameters = []
ActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a Array, but was a String. -- "inappropriate type"
这是预期的行为吗?
2.2.3 :003 > a = TestSerialize.new
=> #<TestSerialize id: nil, parameters: [], first_name: {}, last_name: "", created_at: nil, updated_at: nil>
2.2.3 :004 > a.parameters
=> []
2.2.3 :005 > a.parameters = "test"
ActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a Array, but was a String. -- "test"
2.2.3 :006 > a
ActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a Array, but was a String. -- "test"
2.2.3 :007 > a.parameters
ActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a Array, but was a String. -- "test"
2.2.3 :008 > a.first_name
=> {}
2.2.3 :009 > reload!
Reloading...
=> true
2.2.3 :010 > a
ActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a Array, but was a String. -- "test"