Use reports_count so that counter_cache works automatically.
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 6 Sep 2017 16:57:54 +0000 (17:57 +0100)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 6 Sep 2017 16:57:54 +0000 (17:57 +0100)
app/models/report.rb
app/views/issues/index.html.erb
db/migrate/20160822153153_add_reports_count_to_issues.rb [moved from db/migrate/20160822153153_add_report_count_to_issues.rb with 59% similarity]

index fa90cf0..477b4b1 100644 (file)
@@ -1,4 +1,4 @@
 class Report < ActiveRecord::Base
-  belongs_to :issue
+  belongs_to :issue, :counter_cache => true
   belongs_to :user, :class_name => "User", :foreign_key => :reporter_user_id
 end
index 0eb2108..f9f2c28 100644 (file)
@@ -19,7 +19,7 @@ Search for a particular issue(s):  <br/>
   <thead>
     <tr>
       <td style="width:40px;"><b> <%= sortable("status") %></b></td>
-      <td style="width:160px;"><b> <%= sortable("report_count", "Number of Reports") %></b></td>
+      <td style="width:160px;"><b> <%= sortable("reports_count", "Number of Reports") %></b></td>
       <td style="width:141px;"><b> <%= sortable("updated_at","Last updated at") %></b></td>
       <td style="width:140px;"><b> <%= sortable("updated_by","Last updated by") %></b></td>
       <td style="width:203px;"><b> Link to reports </b></td>
@@ -31,7 +31,7 @@ Search for a particular issue(s):  <br/>
     <% @issues.each do |issue| %>
       <tr>
         <td><%= issue.status.humanize %></td>
-        <td style="text-align:center;"><%= issue.report_count %></td>
+        <td style="text-align:center;"><%= issue.reports_count %></td>
         <td><%= l(issue.updated_at.to_datetime, :format => :friendly) %></td>
         <td><% if issue.user_updated %> <%= issue.user_updated.display_name %> <% else %> - <% end %></td>
         <td><%= reports_url(issue) %></td>
@@ -1,6 +1,6 @@
-class AddReportCountToIssues < ActiveRecord::Migration
+class AddReportsCountToIssues < ActiveRecord::Migration
   def change
-    add_column :issues, :report_count, :integer, :default => 0
+    add_column :issues, :reports_count, :integer, :default => 0
     add_foreign_key :issues, :users, :column => :updated_by, :name => "issues_updated_by_fkey", :on_delete => :cascade
     add_index :issues, :updated_by
   end