我正在使用 Ruby on Rails 3.2.2。我已经为模型类实现了一个 Mixin 模块Article
,我想self
引用它Article
(即使,例如,如果它在方法的上下文之外声明)。也就是说,我正在尝试执行以下操作:
module MyModule
extend ActiveSupport::Concern
# Note: The following is just a sample code (it doesn't work for what I am
# trying to accomplish) since 'self' isn't referring to Article but to the
# MyModule itself.
include MyModule::AnotherMyModule if self.my_article_method?
...
end
上面的代码会产生以下错误:
undefined method `my_article_method?' for MyModule
如何my_article_method?
在上面运行,以便self
(或其他)引用Article
模型类?