Ruby on rails 如何让“self”在Mixin模块中引用my类(即使它是在方法上下文之外声明的)?
我正在使用RubyonRails 3.2.2。我已经为Ruby on rails 如何让“self”在Mixin模块中引用my类(即使它是在方法上下文之外声明的)?,ruby-on-rails,ruby,ruby-on-rails-3,module,mixins,Ruby On Rails,Ruby,Ruby On Rails 3,Module,Mixins,我正在使用RubyonRails 3.2.2。我已经为文章模型类实现了一个Mixin模块,我想让self引用文章(即使它在方法上下文之外声明)。也就是说,我正在努力做到以下几点: 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 '
文章
模型类实现了一个Mixin模块,我想让self
引用文章
(即使它在方法上下文之外声明)。也就是说,我正在努力做到以下几点:
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\u方法?
,以便self
(或其他内容)引用article
模型类?您可以使用self.included
钩子:
def self.included(klass)
klass.include MyModule::AnotherMyModule if klass.my_article_method?
end
我宁愿把逻辑放在实际的文章
类上。模块不需要知道它所包含的类:
class Article
include MyModule
include MyModule::AnotherModule if self.my_article_method?
end
您可以使用
self.included
hook:
def self.included(klass)
klass.include MyModule::AnotherMyModule if klass.my_article_method?
end
我宁愿把逻辑放在实际的文章
类上。模块不需要知道它所包含的类:
class Article
include MyModule
include MyModule::AnotherModule if self.my_article_method?
end
只需使用
关注
的包含
方法即可:
module MyModule
extend ActiveSupport::Concern
included do
include MyModule::AnotherModule if self.my_article_method?
end
end
只需使用
关注
的包含
方法即可:
module MyModule
extend ActiveSupport::Concern
included do
include MyModule::AnotherModule if self.my_article_method?
end
end
通过使用您的代码,我无法再使用有用且方便的
extend-ActiveSupport::Concern
语句。您认为呢?是Article.my\u Article\u方法吗?
运行时常量?是的(应该是)。我还试图将include
语句移动到include do。。。结束由ActiveSupport::Concern
提供的块,但没有成功:我为#
获得了未定义的方法“我的文章方法”。我想包括来自MyModule
的AnotherModule
,因为逻辑“依赖”关于MyModule
的内容比Article
类更多。通过使用您的代码,我无法再使用有用且方便的extend-ActiveSupport::Concern
语句。您认为呢?是Article.my\u Article\u方法吗?
运行时常量?是的(应该是)。我还试图将include
语句移动到include do。。。结束由ActiveSupport::Concern
提供的块,但没有成功:我为#
获得了未定义的方法“我的文章方法”。我想包括来自MyModule
的AnotherModule
,因为逻辑“依赖”关于MyModule
的内容比Article
类更多。通过使用您的代码,我为#
获得了未定义的方法“my#u Article_method?”,即使当我尝试提升(“#{self}”)
(在包含的do…end
块中)我得到了Article
类名。除了没有class方法的Article之外,您是否将其包含在类中my\u Article\u方法?
?不,包含MyModule
模块的所有类都响应my\u Article\u方法?
方法。好的,在您的类中,在定义self.my\u article\u方法之前的include MyModule
行是否存在?如果是这样,请将其移到后面。要使事情正常进行,请按照您的建议,将定义self.my_article_方法?
语句后移动include MyModule::AnotherModule If self.new.my_article_方法?
(注意new
)到包含的do中。。。结束
block。。。为什么?此外,一般来说,在类中使用这些“前后依赖项”是否“正确”/“正确”?通过使用您的代码,我为
获得了未定义的方法“my#u article_method”,即使当我试图提升(“{self}”)
(在包含的do…end
块中)我得到了Article
类名。除了没有class方法的Article之外,您是否将其包含在类中my\u Article\u方法?
?不,包含MyModule
模块的所有类都响应my\u Article\u方法?
方法。好的,在您的类中,在定义self.my\u article\u方法之前的include MyModule
行是否存在?如果是这样,请将其移到后面。要使事情正常进行,请按照您的建议,将定义self.my_article_方法?
语句后移动include MyModule::AnotherModule If self.new.my_article_方法?
(注意new
)到包含的do中。。。结束
block。。。为什么?此外,一般来说,在类中使用这些“前后依赖项”是否“正确”/“正确”?