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

unscoped has no effect when a scope does not provide a query condition

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • None

      A scope that does not provide a query condition, (e.g. using `without`) defaults to using the default_scope even when unscoped is specified. Test case to reproduce:

      class MyClass
        include Mongoid::Document
      
        default_scope -> { where(foo: 1) }
      
        scope :no_condition, -> { without(:bar) }
        scope :with_condition, -> { where(bar: 1) }
      end 
      > MyClass.unscoped.no_condition
      
      #<Mongoid::Criteria
        selector: {"foo"=>1}
        options:  {:fields=>{"bar"=>0}}
        class:    MyClass
        embedded: false>
      
      > MyClass.unscoped.with_condition
      
      #<Mongoid::Criteria
        selector: {"bar"=>1}
        options:  {}
        class:    MyClass
        embedded: false>
       

      The with_condition scope correctly removes the default_scope, whereas the no_condition scope keeps the default_scope query.

            Assignee:
            jamis.buck@mongodb.com Jamis Buck
            Reporter:
            yads12@gmail.com Vadim Kazakov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: