序列化是用数据库中的YAML完成的。他们没有告诉您的是,它还作为YAML传递给XML序列化程序。最后一个论点
serialize
分配
到
options
应为类型
Hash
.
to_xml
用你自己的实现。借用原始的xmlbuilder对象并将其传递给
选项
class User < ActiveRecord::Base
serialize :options, Hash
def to_xml(*args)
super do |xml|
# Hash#to_xml is unaware that the builder is in the middle of building
# XML output, so we tell it to skip the <?xml...?> instruction. We also
# tell it to use <options> as its root element name rather than <hash>.
options.to_xml(:builder => xml, :skip_instruct => true, :root => "options")
end
end
# ...
end