Commit a89ce191 by Toby Lawrence

Use proper relations instead of forcefully loading relations by hand.

Some of the ways comments are accessed in the User model (when a
complete user object is requested) lead to an N+1 situation because
while we preload the related documents (preloading the parent thread of
  a given comment, in this case), further accesses to those comments
  actually never take advantage of the preloaded relations.

We should be using the relation directly, which will still act the same
as before but allow us to actually benefit from preloading.
parent bfd866c1
...@@ -117,35 +117,22 @@ class Comment < Content ...@@ -117,35 +117,22 @@ class Comment < Content
end end
def commentable_id def commentable_id
#we need this to have a universal access point for the flag rake task return nil unless self.comment_thread
if self.comment_thread_id self.comment_thread.commentable_id
t = CommentThread.find self.comment_thread_id
if t
t.commentable_id
end
end
rescue Mongoid::Errors::DocumentNotFound rescue Mongoid::Errors::DocumentNotFound
nil nil
end end
def group_id def group_id
if self.comment_thread_id return nil unless self.comment_thread
t = CommentThread.find self.comment_thread_id self.comment_thread.group_id
if t
t.group_id
end
end
rescue Mongoid::Errors::DocumentNotFound rescue Mongoid::Errors::DocumentNotFound
nil nil
end end
def context def context
if self.comment_thread_id return nil unless self.comment_thread
t = CommentThread.find self.comment_thread_id self.comment_thread.context
if t
t.context
end
end
rescue Mongoid::Errors::DocumentNotFound rescue Mongoid::Errors::DocumentNotFound
nil nil
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment