0

列の合計を作成する必要があります。

問題は、私が performance_reports を取るクラス、アクションにあると思います。 関連付けがあるので、次のコードでユーザーに関連するすべてのレポートを取得します。

    @performance_reports = Array.new
     current_user.websites.each do |website|
      reports = website.performance_reports
        reports.each do |report|
        @performance_reports.push(report)
      end
    end

私の見解では、各レポートを印刷しており、下部に列の要約を印刷したいと考えています。

ここにビューがあります:

   <% @performance_reports.each do |performance_report| %>
      <tr>
        <td><%= performance_report.impressions %></td>
        <td><%= performance_report.clicks %></td>
        <td><%= performance_report.conversions %></td>
      </tr>
     <% end %>
     <tr>
      <td><%=  @performance_reports.count(:clicks)%></td>
      <td><%=  @performance_reports.count(:conversions)%></td>
     </tr>

すべて - クリック、コンバージョン - は整数型です。このコードは印刷中です。その概要は 0 です。

2 つの質問があります。整数を合計する方法と、データベースに保存されていない整数以外の列を合計する方法です。

4

1 に答える 1

2

これには sum メソッドを使用できます

@performance_reports.sum(:clicks)

また、整数以外の型を返すため、整数以外の合計も合計し、このような整数呼び出しで変換します

@performance_reports.sum(:clicks).to_i
于 2012-09-05T13:48:14.433 に答える