comments: minimize sql queries.
Certain parts of comment rendering triggered sql queries that we didn't really need to do. Rework things to avoid this. * Preload comment creators in order to display commenter names with link_to_user. * Preload comment votes in order to display "undo vote" links. Only preload votes for members since anonymous users can't vote and don't have "undo vote" links. * Rework various conditionals to do the filtering in Ruby so that we avoid issuing any extra queries in sql. * Avoid issuing any queries at all when the post doesn't have any comments (when last_commented_at is blank).
This commit is contained in:
@@ -74,7 +74,10 @@ private
|
||||
|
||||
def index_by_post
|
||||
@posts = Post.where("last_comment_bumped_at IS NOT NULL").tag_match(params[:tags]).reorder("last_comment_bumped_at DESC NULLS LAST").paginate(params[:page], :limit => 5, :search_count => params[:search])
|
||||
@posts.each # hack to force rails to eager load
|
||||
|
||||
@posts = @posts.includes(comments: [:creator])
|
||||
@posts = @posts.includes(comments: [:votes]) if CurrentUser.is_member?
|
||||
|
||||
respond_with(@posts) do |format|
|
||||
format.xml do
|
||||
render :xml => @posts.to_xml(:root => "posts")
|
||||
|
||||
@@ -23,6 +23,10 @@ class PostsController < ApplicationController
|
||||
def show
|
||||
@post = Post.find(params[:id])
|
||||
|
||||
@comments = @post.last_commented_at.present? ? @post.comments : Comment.none
|
||||
@comments = @comments.includes(:creator)
|
||||
@comments = @comments.includes(:votes) if CurrentUser.is_member?
|
||||
|
||||
include_deleted = @post.is_deleted? || (@post.parent_id.present? && @post.parent.is_deleted?) || CurrentUser.user.show_deleted_children?
|
||||
@parent_post_set = PostSets::PostRelationship.new(@post.parent_id, :include_deleted => include_deleted)
|
||||
@children_post_set = PostSets::PostRelationship.new(@post.id, :include_deleted => include_deleted)
|
||||
|
||||
Reference in New Issue
Block a user