You can do this via: Neither of these scale, because even with pagination it must still evaluate the subquery for each record in the DB to determine that order or apply that filter.
No calculation, not extra SQL, just a simple read of an attribute on the author record when outputting your list. Hooks are set in the Book class which issue a simple SQL UPDATE statement to increment or decrement the counter cache field. While this is all wonderfully efficient it’s not without complexity costs.Here's the revised code for setting up authors and books: To learn more about the different types of associations, read the next section of this guide.That's followed by some tips and tricks for working with associations, and then by a complete reference to the methods and options for associations in Rails.At the core, we are still executing a count for each row, but we are letting the DB do it while pulling the list. the user has found an item from the list and wishes to drill down.Not quite as efficient as a counter cache, but it still executes very quickly. What about when you want to display just a single record and the count with it? You want to display the count in the detail record as well.