daily pastebin goal
49%
SHARE
TWEET

Untitled

a guest May 16th, 2018 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ==== Error
  2.  
  3. ActiveRecord::StatementInvalid (PGError: ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list
  4. LINE 1: ...comments ON comments.post_id = posts.id  ORDER BY comments.c...
  5.                                                              ^
  6. : SELECT distinct posts.* FROM "posts"  INNER JOIN comments ON comments.post_id = posts.id  ORDER BY comments.created_at DESC LIMIT 5):
  7.   app/models/comment_activity.rb:18:in `find_recent'
  8.   app/controllers/admin/dashboard_controller.rb:4:in `show'
  9.  
  10. ===== Controller
  11.  
  12. class Admin::DashboardController < Admin::BaseController
  13.   def show
  14.     @posts            = Post.find_recent(:limit => 8)
  15.     @comment_activity = CommentActivity.find_recent
  16.     @stats            = Stats.new
  17.   end
  18. end
  19.  
  20. ===== Method
  21.  
  22. class CommentActivity
  23.   attr_accessor :post
  24.  
  25.   def initialize(post)
  26.     self.post = post
  27.   end
  28.  
  29.   def comments
  30.     @comments ||= post.approved_comments.find_recent(:limit => 5)
  31.   end
  32.  
  33.   def most_recent_comment
  34.     comments.first
  35.   end
  36.  
  37.   class << self
  38.     def find_recent
  39.       Post.find(:all,
  40.         :select => 'distinct posts.*',
  41.         :joins  => 'INNER JOIN comments ON comments.post_id = posts.id',
  42.         :order  => 'comments.created_at DESC',
  43.         :limit  => 5
  44.       ).collect {|post|
  45.         CommentActivity.new(post)
  46.       }.sort_by {|activity|
  47.         activity.most_recent_comment.created_at
  48.       }.reverse
  49.     end
  50.   end
  51. end
RAW Paste Data
Top