开发者

Serialization problem in Rails

When we try to deserialize a Model from our database we always receive a YAML object. For that we added the followi开发者_开发百科ng code in the environment.rb:

YAML.add_domain_type("ActiveRecord,2007", "") do |type, val|
  klass = type.split(":").last.constantize
  YAML.object_maker(klass, val)
end

class ActiveRecord::Base
  def to_yaml_type
    "!ActiveRecord,2007/#{self.class}"
  end
end

class ActiveRecord::Base
  def to_yaml_properties
    ['@attributes']
  end
end

This works! But only once, when I refresh the screen I always undefined method ... for YAML. It seems like my code isn't executed anymore...

Can anyone help?

Thnx!


  1. it's not a good idea to serialize a full active record object. The object might change in the meantime and, when you load it, you might find yourself working with a stale object.
  2. be sure the class definition of the object you are deserializing is loaded before the object is deserialized. Normally, you won't need to require the class because it's automatically loaded by Ruby when you try to use it. This doesn't happen when you deserialize an object.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜