如何获得此ActiveRecord :: Associations :: CollectionProxy的总和?

时间:2017-03-24 20:13:50

标签: ruby-on-rails activerecord

[#<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加起来。

我试试map(&amp;:value_charged).sum 搜集.. 什么都没有:(

谢谢

1 个答案:

答案 0 :(得分:1)

假设您有@object has_many关系detailsdetails有字段调用:value_charged,您可以尝试将所有:value_charged的{​​{1}}:

@object.details