0

列を合計したい。

私のコントローラーで

         def index
          @performance_reports = PerformanceReport.all
         end

私のエラー:

       undefined method `+' for #<PerformanceReport:0x4a55690>
74:         <td><%=  @performance_reports.sum(:clicks)%></td>

なにが問題ですか ?

4

2 に答える 2

4

試す

 @performance_reports = PerformanceReport.select('*')

ビューで

<td><%=  @performance_reports.sum(:clicks)%></td>

基本的 PerformanceReport.allにテーブル全体をロードし、配列でクエリをチェーンすることArrayPerformanceReportできません!!!

PerformanceReport.select('*')が返さ ActiveRecord::Relationれ、リレーションで任意の AR メソッドをチェーンできます

Rails lazing loading strategy Lazy loading (will_paginate sample)Rails Query Interfaceを読むことをお勧めします

素晴らしいレール

于 2012-06-21T08:23:13.917 に答える
1

これsumは ActiveRecord メソッドであるため、既に選択されているオブジェクトには使用できません! ypu でできること:

PerformanceReport.sum(:clicks)

データベースが照会されるためです。

于 2012-06-21T08:22:12.037 に答える