From 6e62a4cc33e82f1dca56ffb52d634b0d2eef61f5 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 3 Jan 2017 18:14:56 +0000 Subject: [PATCH 1/2] Optimize Post#favorited_users. Load users in one query rather than one query per user. --- app/models/post.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index e138fc230..4902d9e07 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -965,9 +965,7 @@ class Post < ActiveRecord::Base end def favorited_users - favorited_user_ids.map {|id| User.find(id)}.select do |x| - !x.hide_favorites? - end + User.find(favorited_user_ids).reject(&:hide_favorites?) end def favorite_groups(active_id=nil) From 4e8006ae3baaf8d75c1524f14ae465e15bb7d88c Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 3 Jan 2017 20:01:14 +0000 Subject: [PATCH 2/2] Optimize rendering of post commenter names. Eager load post comments' creators and updaters. Fixes an N+1 queries problem when rendering commenter names at: views/comments/partials/show/_comment.html.erb:6 views/comments/partials/show/_comment.html.erb:20 while rendering /posts/:id pages. --- app/models/post.rb | 2 +- app/views/comments/partials/show/_comment.html.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 4902d9e07..fd39be66e 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -44,7 +44,7 @@ class Post < ActiveRecord::Base has_many :versions, lambda {order("post_versions.updated_at ASC, post_versions.id ASC")}, :class_name => "PostVersion", :dependent => :destroy has_many :votes, :class_name => "PostVote", :dependent => :destroy has_many :notes, :dependent => :destroy - has_many :comments, lambda {order("comments.id")}, :dependent => :destroy + has_many :comments, lambda {includes(:creator, :updater).order("comments.id")}, :dependent => :destroy has_many :children, lambda {order("posts.id")}, :class_name => "Post", :foreign_key => "parent_id" has_many :disapprovals, :class_name => "PostDisapproval", :dependent => :destroy has_many :favorites, :dependent => :destroy diff --git a/app/views/comments/partials/show/_comment.html.erb b/app/views/comments/partials/show/_comment.html.erb index cbfe8323a..dd27351f6 100644 --- a/app/views/comments/partials/show/_comment.html.erb +++ b/app/views/comments/partials/show/_comment.html.erb @@ -1,6 +1,6 @@ <% if CurrentUser.is_moderator? || !comment.is_deleted? %> -
+

<%= link_to_user comment.creator %>