Rails: three most recent comments with unique users

Posted by Dennis Collective on Stack Overflow See other posts from Stack Overflow or by Dennis Collective
Published on 2010-05-25T01:44:01Z Indexed on 2010/06/06 20:42 UTC
Read the original article Hit count: 325

what would I put in the named scope :by_unique_users so that I can do Comment.recent.by_unique_users.limit(3), and only get one comment per user?

class User
  has_many :comments
end

class Comment
  belongs_to :user
  named_scope :recent, :order => 'comments.created_at DESC'
  named_scope :limit, lambda { |limit| {:limit => limit}}
  named_scope :by_unique_users
end

on sqlite named_scope :by_unique_user, :group => "user_id" works,

but makes it freak out on postgres, which is deployed on production PGError: ERROR: column "comments.id" must appear in the GROUP BY clause or be used in an aggregate function

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby