ruby-on-rails - rails : Is that possible to define named scope in a module?

标签 ruby-on-rails ruby-on-rails-3 named-scope

假设有 3 个模型:A、B 和 C。每个模型都有 x属性。

是否可以在模块中定义命名范围并将该模块包含在 A、B 和 C 中?

我尝试这样做并收到一条错误消息,指出 scope不被认可...

最佳答案

是的

module Foo
  def self.included(base)
    base.class_eval do
      scope :your_scope, lambda {}
    end
  end
end

关于ruby-on-rails - rails : Is that possible to define named scope in a module?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4699343/

相关文章:

ruby-on-rails - Sass 构建的 app/builds/application.css 没有进入标题

ruby-on-rails - 当 gemspec 声明开放约束时,为什么 bundler 坚持使用确切的 gem 版本号?

ruby-on-rails - 基于关联的 Rails 顺序

ruby-on-rails - 新手问题,这里的 'yield'是什么意思?

ruby-on-rails - Rails 4:在ElasticSearch索引上从Wysiwyg编辑器清除文本

ruby-on-rails - 从记录关联中仅检索 'id' 值

ruby-on-rails - 使用 has_many 关联保存模型

ruby-on-rails - ruby rails : How to add a form text field if the corresponding filed in the model doesn't exist?

ruby-on-rails - 访问范围时未定义方法 `default_scoped?'

ruby-on-rails - Rails Workflow Gem - 将事件元编程到named_scopes?