Mongoid unscoped模型导致其他模型无范围

时间:2017-01-07 00:22:33

标签: ruby mongoid activemodel

我在Mongoid中使用默认范围,并且看到了一些奇怪的行为。

我有两个模型,称为类别和项目。项目belongs_to类别,虽然在测试中,这似乎无关紧要。两个模型都有一个状态字段(名称不同),并且我在两个与此状态字段相关的模型上都有默认范围。

当我将一个块传递给Category.unscoped时,它也会从Item中删除默认范围。这是一个示例调用网站的样子:

Category.unscoped do
  # these are equal, when I do not want them to be
  Item.count == Item.unscoped.count

  # these are unequal, even though I want them to be the same
  Item.count != Item.where({status: {"$ne" => "trash"}}).count
end

我的问题是:有没有办法使用默认范围,允许Category.unscoped从类别中删除默认范围,不用删除默认范围其他型号,如Item?

1 个答案:

答案 0 :(得分:0)

有关信息,此问题已在最新发行的mongoid中得到解决:

without_default_scope suppresses default scopes of other models