我已经看过很多有关此事的帖子,但似乎都没有解决我的问题。我在模型上有一个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
我有没有错过的方法,或者我是否必须使用类方法来定义此范围?
答案 0 :(得分:6)
似乎没有办法做到这一点。我在github上的rails repo上打开了一个问题...
答案 1 :(得分:0)
试试这个
scope :inactive, lambda { unscoped.where(:is_active => false) }