Order by count

3 views
Skip to first unread message

Ernst Bauernfeind

unread,
Oct 27, 2008, 1:27:50 PM10/27/08
to DataMapper
Hi,
first of all, I want to say that Datamapper is great.
But I have a question.
Let's say I have the following models:

class Post
include DataMapper::Resource
property :id, Integer, :serial => true
property :content, Text
has n, :comments
end

class Comment
include DataMapper::Resource
property :id, Integer, :serial => true
property :content, Text
belongs_to :post
end


Is it possible to make a query via Datamapper like "give me the posts
with the most comments"

in sql it would be:
SELECT posts.*
FROM posts JOIN comments on posts.id=comments.post_id
GROUP BY posts.id
ORDER BY count(comment.id) DESC
LIMIT 10

can I make a query like that in datamapper without having an explicit
"comment_count" field?

thanks!
Ernst
Reply all
Reply to author
Forward
0 new messages