2017-04-26 176 views
0

所以我有一個AR模型,看起來像這樣(所有字符串和日期屬性):如何合併mongodb查詢結果?

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子句?

回答

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 
+0

嘗試這樣做,在我的ERB顯示時,只顯示這PARAM下下降的項目(:paid_on_helper =>零,:assigned_driver2_id.ne =>無) – jdog

+1

嘗試調用'。解釋'在您的查詢。這應該顯示實際的mongo查詢並可以幫助您進行調試。 – mikwat

+0

我做了,現在我的應用程序崩潰了。它會重新啓動,然後不斷崩潰。跆拳道? – jdog