From 04d6084d7409ee4008b1b192d75311aef7fae839 Mon Sep 17 00:00:00 2001 From: Carl Chenet Date: Sat, 20 May 2017 15:36:33 +0200 Subject: [PATCH] eager-load comment votes and their users when showing threads --- app/controllers/comments_controller.rb | 2 +- app/controllers/stories_controller.rb | 4 ++-- app/models/comment.rb | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 67b8a7e..5987581 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -286,7 +286,7 @@ class CommentsController < ApplicationController comments = Comment.where( :thread_id => thread_ids ).includes( - :user, :story + :user, :story, :hat, :votes => :user ).arrange_for_user( @showing_user ) diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 060304a..2783824 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -134,8 +134,8 @@ class StoriesController < ApplicationController @short_url = @story.short_id_url - @comments = @story.merged_comments.includes(:user, :story, - :hat).arrange_for_user(@user) + @comments = @story.merged_comments.includes(:user, :story, :hat, + :votes => :user).arrange_for_user(@user) @force_show_thread_id = nil diff --git a/app/models/comment.rb b/app/models/comment.rb index 4d1029e..9250c46 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -449,7 +449,8 @@ class Comment < ActiveRecord::Base def vote_summary_for_user(u) r_counts = {} r_users = {} - self.votes.includes(:user).each do |v| + # don't includes(:user) here and assume the caller did this already + self.votes.each do |v| r_counts[v.reason.to_s] ||= 0 r_counts[v.reason.to_s] += v.vote