我在Ruby1.9.3上使用delayed_job (3.0.3)时遇到了问题。之前我们使用的是Ruby1.8.7,它带有yaml syck解析器,可以读取为ruby对象(包括attr_accessors)设置的所有属性,但随着升级到1.9.3,yaml解析器切换到了psych (重写),它不考虑任何属性,除了那些持久保存在数据库中的属性。我们怎样才能让心理也考虑到attr_accessors呢?我尝试切换到syck thru:
YAML::ENGINE.yamler = 'syck'但还是不能工作。
有谁有解决这个问题的办法吗?
发布于 2012-11-13 22:27:44
上面的方法不起作用,但我们所需要的就是覆盖ActiveRecord::Base的encode_with和init_with方法,以包含属性访问器。更准确地说,我们需要使用att_accessors来设置编码器散列,这会考虑到实例变量的持久性。
发布于 2012-11-28 09:40:23
delayed_job反序列化程序不对加载的ActiveRecord对象调用init_with。
下面是delayed_job的一个猴子补丁,它在结果对象上调用init_with:https://gist.github.com/4158475
例如,对于猴子补丁,如果我有一个名为Artwork的模型,并具有额外的路径和深度属性:
class Artwork < ActiveRecord::Base
def encode_with(coder)
super
coder['attributes']['path'] = self['path']
coder['attributes']['depth'] = self['depth']
end
def init_with(coder)
super
if coder['attributes'].has_key? 'path'
self['path'] = coder['attributes']['path']
end
if coder['attributes'].has_key? 'depth'
self['depth'] = coder['attributes']['depth']
end
self
end
endhttps://stackoverflow.com/questions/13125777
复制相似问题