查找所有帖子超过规定的评论数 [英] Find all posts with more than a defined number of comments
本文介绍了查找所有帖子超过规定的评论数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的Rails 3博客的应用程序,其中的帖子有很多的意见和评论属于职务。
I have a simple rails 3 blog app where posts have many comments and comments belong to a post.
我要创建范围,将获取的有超过5个评论的所有帖子。 什么是这样做没有计数器缓存列的最佳方式。
I want to create a scope that will fetch all posts that have more than 5 comments. What's the best way of doing this without a counter cache column.
推荐答案
就是这样,也许?
Post.select('posts.*, count(comments.id) as comment_count').
joins(:comments).
group('posts.id').
having('comment_count > 5')
这篇关于查找所有帖子超过规定的评论数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文