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

Strange behavior of 'or' in scope

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

      Say, I have

        scope :active1, -> { where(:'$or' => [{:beg_time => nil}, {:beg_time.gte => Time.now}]) }
        scope :active2, -> { where(:'$or' => [{:beg_time => nil}, {:beg_time => {:'$gte' => Time.now}}]) 
      

      Then, I do

      1.9.3p125 :029 > Item.active1.count
       => 8 
      1.9.3p125 :030 > Item.active2.count
       => 12 
      

      The respective queries are as follows:

        MOPED: 127.0.0.1:27017 COMMAND      database=development command={:count=>"items", :query=>{"$or"=>[{"beg_time"=>nil}, {"#<Origin::Key:0x007fc41b045628>"=>2013-01-21 06:54:06 UTC}]}} (0.8180ms)
        MOPED: 127.0.0.1:27017 COMMAND      database=development command={:count=>"items", :query=>{"$or"=>[{"beg_time"=>nil}, {"beg_time"=>{:$gte=>2013-01-21 06:54:09 UTC}}]}} (1.2159ms)
      

      It appears that the first variant (ie :beg_time.gte) does not get converted into a correct query

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

              Created:
              Updated:
              Resolved: