如何告诉Ruby不要序列化属性或如何正确地重载marshal_dump?

我的AR中有一个属性:B不可序列化。

o = Discussion.find(6) Marshal.dump(o) TypeError: no marshal_dump is defined for class Proc from (irb):10:in `dump' 

我知道罪魁祸首和我想要的是在任何序列化发生之前将此变量设置为nil。

我可以做到这一点,但我坚持使用正确的方法来覆盖marshal_dump

  def marshal_dump @problem = nil # what is the right return here? end 

或者是否有办法告诉Ruby或AR不要序列化对象?

您的专用marshal_dump应返回包含您要序列化的数据的对象。 该对象将在加载时传递回marshal_load

在这种情况下,我假设您要转储的数据对应于所有AR属性(并且只有那些),所以我尝试:

 def marshal_dump attributes end def marshal_load(data) send :attributes=, data, false # false to override even protected attributes end