Rails:可以在模块中定义命名范围吗? [英] Rails: Is that possible to define named scope in a module?

查看:33
本文介绍了Rails:可以在模块中定义命名范围吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Say there are 3 models: A, B, and C. Each of these models has the x attribute.

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

Is that possible to define a named scope in a module and include this module in A, B, and C ?

我尝试这样做,但收到一条错误消息,指出无法识别 scope...

I tried to do so and got an error message saying that scope is not recognized...

推荐答案

是的

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

这篇关于Rails:可以在模块中定义命名范围吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆