Ruby on rails 重写Rails中的多个默认作用域之一

Ruby on rails 重写Rails中的多个默认作用域之一,ruby-on-rails,activerecord,ruby-on-rails-4,rails-activerecord,Ruby On Rails,Activerecord,Ruby On Rails 4,Rails Activerecord,我有两个模块添加到对象的默认范围 module Testable extend ActiveSupport::Concern included do default_scope { where(test_only: false) } end end module ActiveScoped extend ActiveSupport::Concern included do default_scope { where(active: true) } en

我有两个模块添加到对象的默认范围

module Testable
  extend ActiveSupport::Concern

  included do
    default_scope { where(test_only: false) }
  end
end

module ActiveScoped
  extend ActiveSupport::Concern

  included do
    default_scope { where(active: true) }
  end
end
当我在一个模型类中包含这两个模块时,查询在
的where
中包含这两个子句。这两个范围都必须是默认范围的原因是,必须应用这些范围的地方比不应用的地方多得多。此外,忘记在其中一个地方应用范围的成本很高。因此,在少数不适用的地方显式指定似乎是更好的选择。但是,我现在需要编写一个方法,该方法可以从一个模型中获取所有对象,并且只应用其中一个作用域。我该怎么做


任何帮助都将不胜感激。

我不喜欢在模块中定义
默认范围,然后将其包括在内的想法。这听起来更像是模型本身的工作

但是,您仍然可以使用
unscoped
删除这些默认作用域

Foo.all
# Returns relation object with current two default scopes

Foo.unscoped
# Returns all results without any scope

Foo.unscoped do
  where(active: true)
end
# Returns object with only one scope applied

正如我今天发现的,您还可以在
unscope
Foo.unscope(where::active).limit(10)