0

Suppose I have Posts and posts' Comments. I want to filter all the Posts that have more than 10 comments. I began writing something like Posts.includes(:comments).group("post.id").count("comments.id"), to obtain a hash of posts and their counts, and I can extract the information from there, but I want some one-line straightforward way to do that

Sure I can use some pure sql syntax statements, but I want it in a pure rails way. Any idea ?

1
  • Given that you accepted the answer, you might want to update your question to use the correct constants (e.g. Post, Comment), after which I'll update my answer to remove the reference to this. :-) Commented Dec 7, 2013 at 17:51

1 Answer 1

2

Assuming the models are named in the more typical singular form of Post and Comment and have the usual association relationship, then the following should work:

Post.joins(:comments).group('posts.id').having('count(comments.id) > 10')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.