2017-03-24 46 views
0
[#<ActiveRecord::Associations::CollectionProxy [#<InvoiceServiceType id: 1, value_charged: 50.0, invoice_id: 4, service_type_id: 1>, #<InvoiceServiceType id: 2, value_charged: 50.4, invoice_id: 4, service_type_id: 2>]>, #<ActiveRecord::Associations::CollectionProxy [#<InvoiceServiceType id: 8, value_charged: 70.0, invoice_id: 1, service_type_id: 2>, #<InvoiceServiceType id: 9, value_charged: 50.0, invoice_id: 1, service_type_id: 6>]>] 

我想總結所有value_charged。如何獲得這個ActiveRecord :: Associations :: CollectionProxy的總和?

IM嘗試地圖(&:value_charged)的.sum 收集.. 並沒有什麼:(

謝謝

+0

你有什麼錯誤?顯示你的代碼。 – coreyward

回答

0

讓說你有一個has_many關係details一個@object,並details有現場通話:value_charged,你可以嘗試總結的@object.details所有:value_charged有:

@object.details.sum(:value_charged) 
相關問題