如何告诉 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'
我知道罪魁祸首,我想要的是将这个变量设置为零 在进行任何序列化之前。
我可以做到这一点,但我坚持使用正确的方法来覆盖 marshal_dump
def marshal_dump
@problem = nil
# what is the right return here?
end
或者有没有办法告诉 Ruby 或 AR 不要序列化对象?
I have an attribute in my AR:B that is not serializeable.
o = Discussion.find(6)
Marshal.dump(o)
TypeError: no marshal_dump is defined for class Proc
from (irb):10:in `dump'
I know the culprit and what I want is to set this variable to nil
before any serialization takes place.
I can do this but I'm stuck with the proper way to override marshal_dump
def marshal_dump
@problem = nil
# what is the right return here?
end
Or is there is way to tell Ruby or AR not to serialize an object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的专用
marshal_dump
应返回一个包含您要序列化的数据的对象。该对象将在加载时传回marshal_load
。在这种情况下,我假设您要转储的数据对应于所有 AR 属性(并且仅对应于这些属性),所以我会尝试:
Your specialized
marshal_dump
should return an object containing the data you want to serialize. That object will be passed back tomarshal_load
at load time.In this case, I'm assuming the data you want to dump corresponds to all AR attributes (and only those), so I'd try: