Rails 3.1.3无范围范围

时间:2012-01-04 21:59:52

标签: ruby-on-rails activerecord ruby-on-rails-3.1 default-scope

我已经看过很多有关此事的帖子,但似乎都没有解决我的问题。我在模型上有一个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

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

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:0)

试试这个

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