Ruby on rails 为序列化属性赋值会导致SerializationTypeMitch不匹配
我感到惊讶的是,这还没有被问到,所以我希望我做了一些根本错误的事情,帮助很快就会到来。我有这个Ruby on rails 为序列化属性赋值会导致SerializationTypeMitch不匹配,ruby-on-rails,serialization,activerecord,Ruby On Rails,Serialization,Activerecord,我感到惊讶的是,这还没有被问到,所以我希望我做了一些根本错误的事情,帮助很快就会到来。我有这个 create_table "foo", :force => true do |t| t.text "bar", :null => false ... end class Foo < AR::Base serialize :bar, Bar end class Bar def ini
create_table "foo", :force => true do |t|
t.text "bar", :null => false
...
end
class Foo < AR::Base
serialize :bar, Bar
end
class Bar
def initialize(hsh)
@data = hsh # and some more code...
end
end
create_表“foo”,:force=>true do|t|
t、 文本“条”,空=>false
...
结束
类Foo
现在我加上
class Foo < AR::Base
before_validation :get_bar, :on => :create
def get_bar
self.bar = Bar.new({})
end
end
类Foo问题是如何摆脱它。创建迁移时,需要设置默认值
create_table "foo", :force => true do |t|
t.text "bar", :null => false, :default => Bar.new({}).to_yaml
...
end
是的,这是我能够找到的一种解决方案,另一种方法是将default设置为null,这样rails就什么都不做,一切都正常。