Rails 4.2.5在SerializationTypeMismatch之后将值重新分配给序列化字段

时间:2016-02-03 10:17:06

标签: ruby-on-rails ruby ruby-on-rails-4

我有一个序列化的专栏

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"

1 个答案:

答案 0 :(得分:0)

得到了Rails团队的答案 https://github.com/rails/rails/issues/23449