- @comment_pages, @comments = paginate(:diary_comments,
- :conditions => conditions,
- :order => "created_at DESC",
- :per_page => 20)
- @page = (params[:page] || 1).to_i
+ @comments = if params[:before]
+ comments.where("diary_comments.id < ?", params[:before]).order(:id => :desc)
+ elsif params[:after]
+ comments.where("diary_comments.id > ?", params[:after]).order(:id => :asc)
+ else
+ comments.order(:id => :desc)
+ end
+
+ @comments = @comments.limit(20)
+ @comments = @comments.includes(:user)
+ @comments = @comments.sort.reverse
+
+ @newer_comments = @comments.count.positive? && comments.exists?(["diary_comments.id > ?", @comments.first.id])
+ @older_comments = @comments.count.positive? && comments.exists?(["diary_comments.id < ?", @comments.last.id])