我有被延伸的另一引擎的類在其初始化像這樣的發動機:當cache_classes = false時,爲什麼在Rails引擎初始化程序中包含錯誤?
module MyApp
class Engine < ::Rails::Engine
initializer 'extend Product' do
AnotherApp::Product.send :include, MyApp::ProductExtender
end
end
end
的ProductExtender
模塊呼籲AnotherApp一些方法::產品時,它包含,例如
module ProductExtender
def self.included(model)
model.send :include, MethodsToCall
end
module MethodsToCall
def self.included(m)
m.has_many :variations
end
end
end
這工作在測試和生產環境,但是當config.cache_classes = false
,它在我拋出一個NoMethodError
當我嘗試調用由ProductExtender定義的東西,像@ product.variations。
不用說,看到我所有的測試都通過了,然後在開發過程中出現錯誤,這是令人不寒而慄的。當我設置cache_classes = true
時,它不會發生,但它讓我懷疑我是否正在做我不該做的事。
我的問題是雙重的:爲什麼會發生這種情況,是否有更好的方法來實現在另一個應用程序對象上擴展/調用方法的功能?
謝謝大家!
感謝您的意見,@mosch。你的帖子讓我思考,所以我做了一些更多的研究,最終找到了config.to_prepare塊,它似乎解決了問題,而無需更改cache_classes設置(請參閱我提交給我自己的問題的答案)。 – joshuarh 2011-05-02 17:17:07