date_trunc and Group by Month in ActiveRecord
Group by month in ActiveRecord with PostgreSQL date_trunc. Tested time zone handling, key types, missing months, and what groupdate adds.
The blog
Real solutions to SQL → ActiveRecord translation problems. Learn why your queries break, how to fix them, and what Rails actually generates.
Group by month in ActiveRecord with PostgreSQL date_trunc. Tested time zone handling, key types, missing months, and what groupdate adds.
Write LIKE and ILIKE in ActiveRecord, escape input with sanitize_sql_like, and see which patterns use an index. Tested on Rails 8.1 and PostgreSQL 16.
Sort NULLs last in Rails: Arel nulls_last, the raw string trap on SQLite, an IS NULL fallback and the PostgreSQL index. Tested on Rails 8.1.1.
New to ActiveRecord? These posts explain the mental model differences that cause bugs.
LEFT JOINs that become INNER JOINs, multiple conditions, and association gotchas
joins filters using SQL JOIN. includes prevents N+1 by eager loading. Using the wrong one creates hidden performance problems. Rails 7/8 with SQL output.
where returns a relation, find_by returns one record or nil. Here's when each is correct, what breaks if you pick wrong, and Rails 7/8 production patterns.
A 5-table ActiveRecord join took 8 seconds. Rewriting it as a subquery took it to 200ms. Here's what changed and why the query planner behaves differently.
N+1 queries, includes() side effects, and when ActiveRecord makes things slower
pick is pluck for one row. See the SQL for pick vs pluck.first, why ORDER BY matters, and when to use find_by or exists? instead.
The database had indexes. ActiveRecord queries weren't using them. Here's how ActiveRecord query patterns bypass indexes and how to check EXPLAIN output.
I added includes() to every association to fix N+1 queries. Performance got worse. Here's why eager loading isn't always the answer and when to use each.
Group by month in ActiveRecord with PostgreSQL date_trunc. Tested time zone handling, key types, missing months, and what groupdate adds.
Write LIKE and ILIKE in ActiveRecord, escape input with sanitize_sql_like, and see which patterns use an index. Tested on Rails 8.1 and PostgreSQL 16.
Sort NULLs last in Rails: Arel nulls_last, the raw string trap on SQLite, an IS NULL fallback and the PostgreSQL index. Tested on Rails 8.1.1.
Write SQL EXISTS and NOT EXISTS in ActiveRecord with Arel, where.missing and the NOT IN NULL trap. Tested on Rails 8.1.1 and PostgreSQL 16.
Use ROW_NUMBER, RANK and LAG in ActiveRecord for latest-per-group and top N queries. Tested SQL and Ruby on Rails 8.1.1 and PostgreSQL 16.
Use with_recursive in Rails 7.2+ to walk trees in one query. Tested on ActiveRecord 8.1.1 and PostgreSQL 16, with depth, path and cycle guards.
Write SQL CASE WHEN in ActiveRecord with select, sum, group, order, in_order_of and Arel. Tested on Rails 8.1.1 with the real SQL output.
ActiveRecord has no union method. Use or, from with a raw UNION, or find_by_sql. Tested on Rails 8.1.1, with the SQL each one runs.
pick is pluck for one row. See the SQL for pick vs pluck.first, why ORDER BY matters, and when to use find_by or exists? instead.
Convert SQL SELECT DISTINCT to ActiveRecord: distinct, pluck, DISTINCT ON and the PostgreSQL ORDER BY error, with tested Rails 8.1 examples.
joins filters using SQL JOIN. includes prevents N+1 by eager loading. Using the wrong one creates hidden performance problems. Rails 7/8 with SQL output.
where returns a relation, find_by returns one record or nil. Here's when each is correct, what breaks if you pick wrong, and Rails 7/8 production patterns.
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.
destroy_all runs callbacks and triggers dependent destroys. delete_all is a single SQL DELETE. Here's when each is correct and what breaks if you pick wrong.
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.
find_by returns nil when no record matches. find raises RecordNotFound. Nil handling, rescue patterns, and when to use each in Rails production. 2026.
The database had indexes. ActiveRecord queries weren't using them. Here's how ActiveRecord query patterns bypass indexes and how to check EXPLAIN output.
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.
A 5-table ActiveRecord join took 8 seconds. Rewriting it as a subquery took it to 200ms. Here's what changed and why the query planner behaves differently.
ActiveRecord was running 47 identical queries per request. Bullet found the pattern. Here's what caused it, how to read Bullet's output, and the fix.