rails3范围,用于has_many关系中的子项数

试图在rails3中做一个范围。

:book has_many :chapters 

我想要范围:长期返回> 10章的书籍。

如何最好地构建此范围(不使用计数器缓存)?

谢谢!

这应该让你去:

 class Book scope :long, joins(:chapters). select('books.id, count(chapters.id) as n_chapters'). group('books.id'). having('n_chapters > 10') end 

有帮助吗?

啊 – 在上面的评论中回答我自己的问题,我不得不把计数放在HAVING中:

 class Book scope :long, joins(:chapters). select('books.id'). group('books.id'). having('count(chapters.id) > 10') end 

在rails 4.0中,此版本有效。 你必须在having子句中count()。 似乎having子句没有看到’as n_chapters’。

另一种方法是制作子查询。 虽然连接更正确(并且可能也会带来更好的性能),但如果组合多个尝试进行分组的范围,最终可能会得到奇怪的结果。 子查询的侵入性要小得多。 对于这个例子,它将是这样的:

 class Book scope :with_chapters_count, -> { select('books.*'). select('(select count(chapters.id) from chapters where chapters.book_id = books.id) as chapters_count') } scope :long, -> { with_chapters_count.where("chapters_count > 10") } end