Rails 3.1.3 无作用域
我看过很多关于此的帖子,但似乎没有一个能解决我的问题。我在模型上有一个 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 技术交流群。

似乎没有办法做到这一点。我在 github 上的 Rails 存储库上打开了一个问题...
There does not seem to be a way to do this. I opened an issue on the rails repo on github...