I've browsed around the source code and apparently comments may be children of other comments but only up to one level: e.g. comment1 belongs to post1 and comment2 belongs to comment1 but comment3 can not belong to comment2 (because the parent of comment2 is already another Comment).
So the query you need to make sounds like this: fetch all parent comments of post1, then fetch all child comments of the previous parent comments, then merge results. Something like:
class Post
# This returns an Array of Comment objects. If you intend to use
# more than the #count method on the result, you should consider
# adding some sort of ordering on the two queries.
def all_comments
parent_comments = Comment.where(:commentable_id => id)
child_comments = Comment.where(:commentable_id => parent_comments.map(&:id))
parent_comments + child_comments
end
end
post = Post.find(1)
post.all_comments.count # The number of all (parent & child) comments of post
If your comment class name is not Comment feel free to change it to whatever (same for Post).