From 52358fa68a84d0df8a216040b1e5eb0fbfdd26c7 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Fri, 11 Dec 2015 15:11:06 -0500 Subject: [PATCH] Fix many N+1 queries --- app/controllers/comments_controller.rb | 5 ++++- app/controllers/posts_controller.rb | 4 ++-- app/controllers/users_controller.rb | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 7e68555..62acebf 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -4,7 +4,10 @@ class CommentsController < ApplicationController before_filter :set_subcreddit def show - @comments = @comment.subtree.arrange(order: :created_at) + @comments = @comment + .subtree + .includes(:post, :user) + .arrange(order: :created_at) end def new diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 8c848ae..beae91e 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -3,11 +3,11 @@ class PostsController < ApplicationController before_filter :set_subcreddit def index - @posts = Post.all + @posts = Post.includes(:subcreddit, :user).all end def show - @comments = @post.comments.arrange(order: :created_at) + @comments = @post.comments.includes(:user).arrange(order: :created_at) end def new diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index c8cd616..283c337 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,7 +1,7 @@ class UsersController < ApplicationController def show @user = User.friendly.find(params[:id]) - @comments = @user.comments + @comments = @user.comments.includes(:post) end def new