eager-load comment votes and their users when showing threads

This commit is contained in:
Carl Chenet 2017-05-20 15:36:33 +02:00
parent 7f5114d0f1
commit 04d6084d74
3 changed files with 5 additions and 4 deletions

View file

@ -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
)

View file

@ -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

View file

@ -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