Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5204

default_scopes duplication, query scope inconsistency

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 7.3.4, 7.4.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • The redundant $and condition would no longer be generated.
    • Minor Change

      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"=>[

      {"lazy"=>true}

      ]}
      ```

      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"=>[

      {"lazy"=>false}

      ]}
      ```

      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

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            pgdstryr@gmail.com Rob Lacey
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: