我有一张发票 list ...
@invoices_1_week = Invoice.order("due_date DESC").where("status != 'paid' AND due_date >= ? AND due_date < ?", Date.today, 1.week.from_now)
发票模型有一个总属性。我怎样才能得到@invoice_1_week 集合中的总和?
我知道我可以在这样的 View 中做到这一点......
<% week_1_total = 0 %>
<% @invoices_1_week.each do |invoice| %>
<% week_1_total = week_1_total + invoice.total %>
<% end %>
<%= week_1_total %>
但我想知道是否有更多的 Railsy 方法来做到这一点。
最佳答案
关于ruby-on-rails - rails 。对集合的特定属性求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10692310/