7

我看过很多关于这个的帖子,但似乎没有一个能解决我的问题。我有一个default_scope这样的模型:

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

我有其他(普通)范围,我想inactive使用unscoped. 我想将它定义为一个范围,但它仅在定义为类方法时才有效:

# 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

有没有我错过的方法,或者我必须使用类方法来定义这个范围?

4

2 回答 2

6

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

于 2012-01-06T19:44:47.417 回答
0

试试这个

scope :inactive, lambda { unscoped.where(:is_active => false) }
于 2012-01-04T22:09:47.987 回答