Rails 3.1.3 无作用域

发布于 12-25 02:25 字数 832 浏览 5 评论 0原文

我看过很多关于此的帖子,但似乎没有一个能解决我的问题。我在模型上有一个 default_scope ,如下所示:

default_scope where(:is_active => true).order('LOWER(table.name)');

我有其他(正常)范围,并且我想使用 unscoped 创建一个 inactive 范围。我想将其定义为范围,但它仅在定义为类方法时才有效:

# works
def self.inactive
  unscoped { where(:is_active => false) }
end

# none of these work
scope :inactive, unscoped { where(:is_active => false) }
scope :inactive, with_exclusive_scope { where(:is_active => true) }
scope :inactive, unscoped.where(:is_active => false)
scope :inactive, lambda { unscoped { where(:is_active => false) } }
scope :inactive, unscoped { lambda { where(:is_active => false) } }
unscoped do
  scope :inactive, where(:is_active => false)
end

是否有我错过的方法,或者我是否必须使用类方法来定义此范围?

I've seen a lot of posts regarding this, but none seem to solve my problem. I have a default_scope on a model like so:

default_scope where(:is_active => true).order('LOWER(table.name)');

I have other (normal) scopes, and I want to create an inactive scope using unscoped. I would like to define it as a scope, but it only works when defined as a class method:

# works
def self.inactive
  unscoped { where(:is_active => false) }
end

# none of these work
scope :inactive, unscoped { where(:is_active => false) }
scope :inactive, with_exclusive_scope { where(:is_active => true) }
scope :inactive, unscoped.where(:is_active => false)
scope :inactive, lambda { unscoped { where(:is_active => false) } }
scope :inactive, unscoped { lambda { where(:is_active => false) } }
unscoped do
  scope :inactive, where(:is_active => false)
end

Is there a way that I missed, or do I have to use a class method to define this scope?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你鎻心2025-01-01 02:25:17

似乎没有办法做到这一点。我在 github 上的 Rails 存储库上打开了一个问题...

There does not seem to be a way to do this. I opened an issue on the rails repo on github...

我的影子我的梦2025-01-01 02:25:17

试试这个

scope :inactive, lambda { unscoped.where(:is_active => false) }

Try this

scope :inactive, lambda { unscoped.where(:is_active => false) }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文