所以我有一个看起来像这样的AR模型(所有字符串和日期属性):
AR driver_id DRIVER_NAME driver_license paid_on(日期) ... helper_id HELPER_NAME helper_license paid_on_helper(日期)
我有这个查询显示未支付的
def self.unpaid
result1 = AR.where(:paid_on => nil).in(:status => [AR::TRANSACTION_COMPLETE,AR::CANCELLED_BY_REQUESTER]).gt(:total_owed_to_driver => 0).asc(:assigned_driver_id).asc(:timestamp_requested)
result2 = AR.where(:paid_on_helper => nil, :assigned_driver2_id.ne => nil).in(:status => [AR::TRANSACTION_COMPLETE,AR::CANCELLED_BY_REQUESTER]).gt(:total_owed_to_driver => 0).asc(:assigned_driver2_id).asc(:timestamp_requested)
result1.to_a.concat(result2.to_a)
result1
end
问题在于,当我以这种方式合并这两个时,我最终得到了这样的数组。
Joe (main driver)
Joe (main driver)
Joe (main driver)
Bob (main driver)
Bob (main driver)
Larry (main driver)
Larry (main driver)
Larry (main driver)
Joe (2nd driver)
Joe (2nd driver)
Bob (2nd driver)
Bob (2nd driver)
Bob (2nd driver)
Bob (2nd driver)
Larry (2nd driver)
Larry (2nd driver)
我需要的是一个像这样结束的数组。
Joe (main driver)
Joe (main driver)
Joe (main driver)
Joe (2nd driver)
Joe (2nd driver)
Bob (main driver)
Bob (main driver)
Bob (2nd driver)
Bob (2nd driver)
Bob (2nd driver)
Bob (2nd driver)
Larry (main driver)
Larry (main driver)
Larry (main driver)
Larry (2nd driver)
Larry (2nd driver)
我猜它的排序结合了二级或三级where子句?
答案 0 :(得分:1)
这可以使用单个mongo查询来完成吗?像这样:
def self.unpaid
AR.or({ :paid_on => nil }, { :paid_on_helper => nil, :assigned_driver2_id.ne => nil })
.in(:status => [AR::TRANSACTION_COMPLETE,AR::CANCELLED_BY_REQUESTER])
.gt(:total_owed_to_driver => 0)
.asc(:assigned_driver_id)
.asc(:timestamp_requested)
end