我正在尝试为模型共享一些常见的范围,所以我创建了这个:
lib
|_ scopes
|_ scope.rb
|_ bars.rb
# lib/scopes/scope.rb
module Scope
end
# lib/scopes/bars.rb
module Scope
module Bars
def self.included(base)
base.class_eval{
scope :somescope, lambda{ where(:foo => :bar) }
}
end
end
end
现在,当我将其放入模型中时:
include Scope::Bars
它抛出一个错误:需要 c:/sites/app_name/lib/scopes/bars.rb 来定义 Bars
出于某种奇怪的原因,当我只使用 Rails 控制台而不是 WEBrick 服务器进行测试时,不会发生这种情况。
如果我不使用子模型,它也可以完美工作,例如:include Bars
就可以很好地工作。
有什么想法吗?
最佳答案
结构如下
lib
|_ scopes
|_ scope.rb
|_ bars.rb
希望您将顶层模块定义为Scopes
,而不是Scope
。将文件夹从 scopes
重命名为 scope
或将模块更改为
# lib/scopes/bars.rb
module Scopes
module Bars
def self.included(base)
base.class_eval do
scope :somescope, lambda { where(:foo => :bar) }
end
end
end
end
关于ruby-on-rails - 无法让嵌套模块工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11261819/