I have a model of a post with has_many comments. Every post has a thread id (but there is no model named Thread).
So if I want to count the number of threads in this post I do smth like
> post.comments.count(:thread, :distinct => true)
SELECT COUNT(DISTINCT "comments"."thread") FROM "comments" WHERE "comments"."post_id" = 3
And this works fine. But what if I want to count the number of threads with only one comment?
> post.comments.group(:thread).having('COUNT(*) == 1').count
SELECT COUNT(*) AS count_all, thread AS thread FROM "comments" WHERE "comments"."post_id" = 3 GROUP BY thread HAVING COUNT(*) == 1 ORDER BY id
So I have a OrderedHash instead of Integer. And I have to do the unnecessary step
> post.comments.group(:thread).having('COUNT(*) == 1').count.count
Is there any better solution?