私は行合計を持つ表を印刷していますが、列の合計も取得したいと思います。次のコードは機能しません。総計の代わりに、最後の反復の値が表示されます。Rails 3.配列からの合計を取得する
<% @shipments.each do |shipment| %>
<tr>
<td style="text-align:center;"><%= shipment.file_number %></td>
<td><%= shipment.shipper.company_name %></td>
<td><%= shipment.hbl %></td>
<td><%= shipment.status %></td>
<td><%= shipment.age %></td>
<td><%= shipment.invoice.read_issued_at unless shipment.invoice.nil? %></td>
<td><%= number_to_currency shipment.invoice.customer_total unless shipment.invoice.nil? %></td>
<td><%= number_to_currency shipment.invoice.customer_amount_paid unless shipment.invoice.nil? %></td>
<td><%= number_to_currency shipment.invoice.customer_open_balance unless shipment.invoice.nil? %></td>
</tr>
<%
grand_customer_total = 0
grand_customer_amount_paid = 0
grand_customer_open_balance = 0
grand_customer_total += shipment.invoice.customer_total
grand_customer_amount_paid += shipment.invoice.customer_amount_paid
grand_customer_open_balance += shipment.invoice.customer_open_balance
%>
<% if @shipments.last == shipment %>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<th>Totals</th>
<td><%= number_to_currency grand_customer_total %></td>
<td><%= number_to_currency grand_customer_amount_paid %></td>
<td><%= number_to_currency grand_customer_open_balance %></td>
</tr>
<% end %>