ActiveRecord group and count — SQL GROUP BY in Rails
How to use group and count in ActiveRecord for SQL GROUP BY. With having, order, and the SQL Rails generates. Rails 7/8 production examples with output.
Guides to translating SQL GROUP BY, HAVING, and aggregate functions into ActiveRecord, including edge cases that break counts and summaries.
How to use group and count in ActiveRecord for SQL GROUP BY. With having, order, and the SQL Rails generates. Rails 7/8 production examples with output.
Added select() to stop loading unused columns. Response times dropped 40%. Here's the pattern, what to watch for with missing attributes, and when worth it.
Using each on large ActiveRecord collections loads all records into memory. find_each batches them. Here's why background jobs need this and the gotchas.
Adding WHERE after LEFT JOIN in Rails silently converts it to INNER JOIN. Here's why it happens, how to spot it, and the fix with .merge() or subqueries.