2015-10-05 47 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} 
) 

希望它有帮助。