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

Chaining "any_of" results in concatenation of conditions

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      I have a bizzarre case of scope chaining...

      I have 2 scopes on model

      Unable to find source-code formatter for language: mymodel```. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      :
      
      

      scope :xxx, any_of({ :aaa.gt => 0 }, { :bbb.gt => 0 })
      scope :yyy, -> { any_of({:ccc => nil}, {:ccc.gt => Time.now}) } # notice the evaluation time

      1. When I do 

      MyModel.xxx

      , I get this correct criteria:
      
      

      => #<Mongoid::Criteria
      selector: {"$or"=>[{:aaa=>{"$gt"=>0.0}}, {:bbb=>{"$gt"=>0.0}}]},
      options: {},
      class: MyModel,
      embedded: false>

      2. When I do 

      MyModel.yyy

      , I get another correct criteria:
      
      

      => #<Mongoid::Criteria
      selector: {"$or"=>[{:ccc=>nil}, {:ccc=>{"$gt"=>2012-07-05 01:58:54 UTC}}]},
      options: {},
      class: MyModel,
      embedded: false>

      3. However, strange things start to happen when I chain them together.
      
      

      MyModel.xxx.yyy

       results in 
      

      {"$or"=>[{:aaa=>{"$gt"=>0.0}}, {:bbb=>{"$gt"=>0.0}}, {:ccc=>nil}, {:ccc=>{"$gt"=>2012-07-05 02:21:53 UTC}}]}

       - ie, instead of doing an 

      AND(OR,OR)

      , I get an 

      OR

       of everything.
      
      Wait, but there is more...
      
      

      MyModel.yyy.xxx

       results in
      

      {"$or"=>[{:aaa=>{"$gt"=>0.0}}, {:bbb=>{"$gt"=>0.0}}]}

       - wherein the 

      yyy

       scope gets ignored altogether
      
      Am I missing something or the chaining of 

      any_of{{`}} is broken?

            Assignee:
            Unassigned Unassigned
            Reporter:
            emirkin Eugene Mirkin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: