2
我的用戶模型中有一個序列化的字段叫做options(類型爲Hash)。選項字段在大多數情況下表現得像哈希。當我將用戶對象轉換爲XML時,'選項'字段被序列化爲YAML類型而不是哈希。ActiveRecord序列化屬性中的XML序列化問題
我正在通過REST將結果發送給Flash客戶端。這種行爲在客戶端造成問題。有沒有辦法解決這個問題?
class User < ActiveRecord::Base
serialize :options, Hash
end
u = User.first
u.options[:theme] = "Foo"
u.save
p u.options # prints the Hash
u.to_xml # options is serialized as a yaml type:
# <options type=\"yaml\">--- \n:theme: Foo\n</options>
編輯:
我正在解決這個問題,通過傳遞塊to_xml(類似於MOLF建議的解決方案)
u.to_xml(:except => [:options]) do |xml|
u.options.to_xml(:builder => xml, :skip_instruct => true, :root => 'options')
end
我不知道是否有更好的方法。 。
我現在正在使用類似的方法。請參閱我編輯的原始問題。由於我不清楚我會爲你的答案投票+1。在你的解決方案中,你需要添加:除了忽略'選項'的參數。否則,最終在串行化的XML字符串中會出現兩個「選項」字段。 – 2010-02-05 22:51:21