optimize database queries for "Your Threads" page

This commit is contained in:
Serge Paquet 2014-01-15 21:17:32 -05:00
parent 7d10b5eb06
commit 02a30c66b1
2 changed files with 31 additions and 36 deletions

View file

@ -229,48 +229,41 @@ class CommentsController < ApplicationController
@cur_url = "/threads"
end
@threads = @showing_user.recent_threads(20).map{|r|
cs = Comment.where(
:thread_id => r
).includes(
:user, :story
).arrange_for_user(
@showing_user
)
thread_ids = @showing_user.recent_threads(20)
if @user && (@showing_user.id == @user.id)
@votes = Vote.comment_votes_by_user_for_story_hash(@user.id,
cs.map{|c| c.story_id }.uniq)
comments = Comment.where(
:thread_id => thread_ids
).includes(
:user, :story
).arrange_for_user(
@showing_user
)
cs.each do |c|
if @votes[c.id]
c.current_vote = @votes[c.id]
end
comments_by_thread_id = comments.group_by(&:thread_id)
@threads = comments_by_thread_id.values_at(*thread_ids).compact
if @user && (@showing_user.id == @user.id)
@votes = Vote.comment_votes_by_user_for_story_hash(@user.id,
comments.map(&:story_id).uniq)
comments.each do |c|
if @votes[c.id]
c.current_vote = @votes[c.id]
end
else
@votes = []
end
cs
}
end
# trim each thread to this user's first response
# XXX: busted
if false
@threads.map!{|th|
th.each do |c|
if c.user_id == @user.id
break
else
th.shift
end
end
th
}
end
@comments = @threads.flatten
#@threads.each do |th|
# th.each do |c|
# if c.user_id == @user.id
# break
# else
# th.shift
# end
# end
#end
end
private

View file

@ -1,5 +1,6 @@
<% @threads.each do |thread| %>
<% indent_level = -1 %>
<% @comments.each_with_index do |comment,x| %>
<% thread.each do |comment| %>
<% if comment.indent_level > indent_level %>
<ol class="comments comments<%= comment.indent_level %>">
<% elsif comment.indent_level < indent_level %>
@ -16,3 +17,4 @@
<% indent_level.times do %>
</ol>
<% end %>
<% end %>