Discovered an oddity and possibly some cause for concern on expected behaviour of default_scopes on subclasses, etc.
The gist is that when you apply a scope to a scope and it is duplicated you see things like this. If you apply the scope once, you get the lazy key in the selector. Apply it twice you get 'lazy' => true in an $and key, more than that it doesn't care.
```
Cat.where(lazy: true).where(lazy: true).where(lazy: true).where(lazy: true).selector
=> {"lazy"=>true, "$and"=>[
]}
```
Ah, but it appears that the value in the $and key is replace.
```
Cat.where(lazy: true).where(lazy: true).where(lazy: true).where(lazy: false).selector
=> {"lazy"=>true, "$and"=>[
]}
```
Why replace it at all, and even if you did why not replace the 'lazy' key? and not the one in the $and. Why not append to the $and Array. The behaviour seems really inconsistent.
Not to mention that if you have a default scope, the scope is always run twice and so this behaviour always occurs in subclasses with default_scope. Why not ignore duplicates entirely?
https://github.com/mongodb/mongoid/discussions/5102
Demonstrated here
https://github.com/braindeaf/mongoid-where-oddity
I hope this makes sense
RobL
- is duplicated by
-
MONGOID-5251 find(_id) generates useless redundant $and in query causing suboptimal query planning
- Closed