Mongoid unscoped model causing other model to be unscoped

221 views Asked by At

I'm using default scopes in Mongoid, and have seen some odd behavior.

I have two models in question, call them Category and Item. Item belongs_to Category, although in testing, this seems to not matter. Both models have a status field (with different names), and I have a default scope on both models related to this status field.

When I pass a block to Category.unscoped, it removes the default scope from Item, as well. Here's what an example callsite looks like:

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

My question is: is there a way to use default scopes which will allow Category.unscoped to remove the default scope from Category, without removing default scopes from other models, such as Item?

1

There are 1 answers

0
Aymeric On

For information this issue has been fixed in recent releases of mongoid:

without_default_scope suppresses default scopes of other models