2015-10-05 34 views
0

我可以成功地創建一個範圍,以找對象,其中一個日期在一個範圍 -合併兩個日期範圍查詢Mongoid

all_matches = Person.where({ 
    :some_date.gte => first_date, 
    :some_date.lte => last_date 
}) 

但是當我使用or代替where它開始返回的數據不匹配任何的標準 -

all_matches = Person.or({ 
    :some_date.gte => first_date, 
    :some_date.lte => last_date 
}, { 
    :some_other_date.gte => first_date, 
    :some_other_date.lte => last_date 
}) 

我看不出有任何理由,我一直在使用之前or但不是單一的數值範圍。

回答

0

您的意圖是讓所有人的some_date或some_other_date在first_date和last_date之間的範圍內。 如果是這樣,請嘗試:

first_date, last_date = [Time.now.to_date, (Time.now + 1.day).to_date] 
Person.or(
    {:some_date => first_date..last_date}, 
    {:some_other_date => first_date..last_date} 
) 

希望它有幫助。