From 72aee6b2c1959a9486bc6e324640445147cbad1d Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Tue, 24 Jan 2012 23:47:45 -0800 Subject: [PATCH] don't use .last(3), use .limit(3) instead. also, don't put an order on an association as it isn't overridable. (this commit minimizes AR object instantiation in the stream) --- app/models/post.rb | 4 +++- lib/diaspora/commentable.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index dc656e672..2657df5ba 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -67,7 +67,9 @@ class Post < ActiveRecord::Base # gives the last three comments on the post def last_three_comments return if self.comments_count == 0 - self.comments.includes(:author => :profile).last(3) + # DO NOT USE .last(3) HERE. IT WILL FETCH ALL COMMENTS AND RETURN THE LAST THREE + # INSTEAD OF DOING THE FOLLOWING, AS EXPECTED (THX AR): + self.comments.order('created_at DESC').limit(3).includes(:author => :profile).reverse! end def self.excluding_blocks(user) diff --git a/lib/diaspora/commentable.rb b/lib/diaspora/commentable.rb index 54f3bc0d7..a603c1835 100644 --- a/lib/diaspora/commentable.rb +++ b/lib/diaspora/commentable.rb @@ -6,7 +6,7 @@ module Diaspora module Commentable def self.included(model) model.instance_eval do - has_many :comments, :as => :commentable, :order => 'created_at', :dependent => :destroy + has_many :comments, :as => :commentable, :dependent => :destroy end end