trying to do a scope in rails3.
:book has_many :chapters
I want scope :long to return books with > 10 chapters.
How best to structure
An alternative is to make a subquery. While the join is more correct (and possibly also leads to better performance), you may end up with strange results if you combine multiple scopes that try to do grouping. A subquery is much less intrusive. For this example, it would be something like:
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