在同一应用程序中使用 identity_cache (v 0.0.4) 和 delayed_job (v 3.0.3) 时,出现错误“#
我跟踪堆栈跟踪,发现 delayed_job 覆盖了 ActiveRecord::Base#encode_with。在 delayed_job 版本中,它调用
coder.tag = ['!ruby/ActiveRecord', self.class.name].join(':')
似乎 ActiveRecord::Base 期望 coder 是一个 Hash,而 delayed_job 假设它是一个 Psych::Coder,它确实有一个 tag= 方法。
最后,identity_cache确实调用了encode_with,传递了一个Hash。因此错误。
我使用的是 ruby 1.9.3p429,它的标准库中包含 psych。我没有在任何地方指定 yaml 解析器引擎,当我检查时它总是返回 psych(有些人提示过类似的问题,但他们使用 syck 进行 yaml 解析)。
所以,我想问题是,如何让 delayed_job 与 identity_cache 配合得很好?
最佳答案
也许不是最优雅的解决方案,但下面的猴子补丁是一个变通方法。到目前为止,我还没有发现任何错误。我把它放在 .../config/initializers/patch_identity_cache.rb 中:
module IdentityCache
module ConfigurationDSL
extend ActiveSupport::Concern
module ClassMethods
alias_method :original_build_normalized_has_many_cache, :build_normalized_has_many_cache
def build_normalized_has_many_cache(association, options)
original_build_normalized_has_many_cache(association, options)
self.class_eval(ruby = <<-CODE, __FILE__, __LINE__)
def #{options[:population_method_name]}
@#{options[:ids_variable_name]} = #{options[:ids_name]}
association_cache.delete(:#{association})
@#{options[:records_variable_name]} = nil
end
CODE
end
end
end
end
关于ruby - 使用 identity_cache 和 delayed_job 的哈希的未定义方法标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19363683/