我有一个模型类attr_reader -type_slugs
(返回一个字符串数组),我想用它来生成几个方法。
举例来说:
module Orders
module Extensions
module Order
module TypeCheckable
extend ::ActiveSupport::Concern
::Orders::Config.type_slugs.each do |type|
define_method("#{type}_order_type?") { order_type == type }
end
end
end
end
end
但它给了我NoMethodError: undefined method
type_slugs' for Orders::Config:Module`
如果我使用任何模型方法,也会发生同样的错误,比如:
some_method_defined_in_model.each do |type|
define_method("#{type}_order_type?") { order_type == type }
end
但是如果我用一个单词数组
%w(message product another).each do |type|
define_method("#{type}_order_type?") { order_type == type }
end
在这种情况下,是否可以使用Orders::Config.type_slugs
?若否,原因为何?
1条答案
按热度按时间xzlaal3s1#
根据您提供的信息,模块
Orders::Config
没有type_slugs
方法如果你想让你的代码工作,你需要定义它,像这样:
或者,如果该列表不动态更改:
在这种情况下,将
::Orders::Config.type_slugs
更改为::Orders::Config::TYPE_SLUGS
当使用Rails concerns时,应该为示例方法使用
included
之后,您可以在模型中使用它
并像这样调用方法