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.
posts.count hit the database on every request and brought down production. Here's how counter cache fixes it, what to watch for, and how to backfill safely.
pluck, select, and map look interchangeable but have very different memory profiles. Here's which one killed our production app and when to use each.
When ActiveRecord's abstraction costs 80% of query performance, raw SQL is the right call. Here's how to know when to drop down and do it safely in Rails.
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.
Added includes() to every N+1 warning. Response times went up, not down. Here's what went wrong and how to choose between eager loading and joins.
A SQL to ActiveRecord converter exists. Leaning on it without understanding means you can't debug what it outputs. Here's how the tool teaches as it converts.
Migrating a legacy PHP app to Rails — the biggest challenge wasn't the syntax or frameworks. It was thinking in relationships instead of tables.
A junior dev asked why we use ActiveRecord instead of raw SQL. I gave the textbook answer. They weren't convinced. Then production happened.
I had SQL certification on my resume and failed a Rails interview. Knowing databases and knowing ActiveRecord are two different skills. Here's the gap.
I read Agile Web Development with Rails cover to cover. I still couldn't write production queries. Here's the gap between theory and what actually works.
SQL has one way to write a query. ActiveRecord has ten. I thought that was flexibility. It was actually paralysis. Here's how I narrowed it down.
I converted 50 SQL queries to ActiveRecord. 43 were subtly wrong. The 7 that worked correctly taught me the patterns that actually matter.
The ActiveRecord version was 3 lines. The SQL version was 10. Understanding why the short version was correct took me 3 weeks. Here's that explanation.
I taught SQL for 5 years. Teaching ActiveRecord broke every analogy I had. Here's what I had to unlearn — and the new model that actually works for beginners.
As a DBA, Rails felt wrong. SQL was cleaner. Then I hit maintenance hell on a raw-SQL codebase and changed my mind. Here's what shifted.
I searched for SQL to ActiveRecord conversion so many times I finally built the tool myself. Here's what I learned about the patterns that repeat constantly.
Every SQL to ActiveRecord converter gave me different output for the same query. Here's why — and what that tells you about how ActiveRecord actually works.
SQL JOINs made sense. ActiveRecord's includes() confused me for weeks. Here's the mental model that finally made it click and when to use each.
My SQL query was perfectly optimized. Porting it to ActiveRecord triggered N+1 queries in production. Here's the exact pattern that caused it and the fix.
find_by returns nil when no match. find raises RecordNotFound. Here's when each is the right choice, nil handling patterns, and production-safe usage. 2026.