Reputation: 46369
I know I can apply a limit to the associations in a has_many :through
, i.e.:
class Post
has_many :commenters, through: :comments, uniq: true, limit: 10
end
This will return a maximum of 10 commenters
. But what if I only want to know people who contributed the first 10 comments? (e.g. if there was a ping-pong comment thread, it would yield only 2 results). In other words, how do I limit the number of comments
in this query?
Upvotes: 0
Views: 258
Reputation: 15515
Perhaps its better to separate the relation definition from the query you want to make.
class Post
has_many :commenters, through: :comments
def last_commenters
comments.order('created_at DESC').limit(10).map{|c|c.commenter}.uniq
end
end
Disclaimer: code not tested.
Upvotes: 1