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

Ability to remove default scope from Criteria

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: Query

      I am upgrading from Mongoid 6.4.8 to 7.4.0. Many changes I have been able to work through, but one persists and is causing a few issues.

      How do I replace a where condition created with the `ne` method? I have a model, User. It has a default scope that, among other conditions, only selects users where they that are not preview users and have accepted their invite (datetime field invite_accepted_at).

      default_scope -> { where(is_preview_user: false, blocked: false).ne(invite_accepted_at: nil) }
      

      In some places, I need to get all users, including those who haven't accepted the invite.

      User.where(invite_accepted_at: nil).count
      

      In 6.4.8, this produced a query that looked like this:

      ```

      {"is_preview_user"=>false, "blocked"=>false, "invite_accepted_at"=>nil }

      ```

      However, in 7.4.0, this produces a query that looks like this:

      ```
      {"is_preview_user"=>false, "blocked"=>false, "invite_accepted_at"=>

      {"$ne"=>nil}

      , "$and"=>[

      {"invite_accepted_at"=>nil}

      ]}
      ```

      As you can see, the 7.4.0 is trying to get users where invite_accepted_at is both nil and not nil, resulting in no users returned.

      So is there a method I can call, a change I can make, or a setting I can enable to generate the right query, namely, the later call to invite_accepted_at replaces the earlier one.

      I tried:

      User.override.where(invite_accepted_at: nil)
      

      but it seems to have no effect.

      Would appreciate being pointed in the right direction.

            Assignee:
            Unassigned Unassigned
            Reporter:
            kieran@xpand.io Kieran Pilkington
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: