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.
My ActiveRecord query looked clean. The SQL was a full table scan. Here's what I missed and why I check EXPLAIN ANALYZE on every non-trivial query now.
SQL fluency creates specific blind spots when learning ActiveRecord. Here's where the mental model breaks, and how to reframe it so the ORM finally makes sense.
When to use select vs pluck in ActiveRecord — memory use, return types, N+1 traps, and the SQL Rails generates for each. With Rails 7/8 examples.
The complete ActiveRecord query interface for Rails 7 and 8: where, joins, includes, select, group, order, limit, offset, and the SQL each generates.
WHERE with hash, string, and .or() syntax — all in one guide. Multiple conditions, AND/OR logic, WHERE vs HAVING, and inspecting SQL output. Rails 7/8, 2026.
Rails 8.2 changes callback ordering for background jobs. Here's what breaks, why it matters, and how to audit your Sidekiq jobs before upgrading.
Step-by-step Rails 8.2 upgrade guide for production apps. Covers the breaking changes, zero-downtime migration patterns, and what to test before you deploy.
Rails 8.2 is thread-safe by default, but your app can still be slow under concurrency. Here's what's actually blocking threads and how to fix it.
Rails 8.2 ActiveRecord changes that break existing apps at scale: deprecated methods, new query behavior, and migration patterns to update safely.
Rails has no native FULL OUTER JOIN. Here are three working approaches: Arel, find_by_sql, and union of LEFT + RIGHT joins. With SQL output for each.
Seven ways to do a CROSS JOIN in Rails without blowing up in production. Covers Arel, raw SQL, CTEs, and the row-count pitfalls to know before you deploy.
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.
Rails doesn't expose CROSS JOIN directly. Here's how to write one with Arel or find_by_sql, when it's the right tool, and when to avoid it in production.
Rails INNER JOIN with multiple ON conditions — without moving predicates into WHERE by accident. .merge(), Arel, and SQL verification. Rails 7/8, 2026.
Ruby 4.0.0 is out. For Rails developers, here's what actually changes: removed APIs, performance gains, and the breaking changes that hit ActiveRecord.
N+1 queries aren't a Rails flaw — they come from how you shape associations. Learn to spot the pattern early and fix it without over-eager loading.
ActiveRecord isn't SQL with Ruby syntax — it's a different mental model. Here's why the shift trips up SQL-fluent developers and how to make it click.
How to structure a Rails 8.1 app as a modular monolith using engines. Clear boundaries without microservice overhead. With real boundary patterns.
Rails 8.1 devcontainer support lets your team share one environment. Here's how to set it up, what it solves, and where it still falls short.
Ruby 3.5 features that actually matter if you run Rails in production: performance wins, concurrency changes, and what you can ignore. Updated 2026.