Rails Joins Return Duplicate Rows? Rewrite Them as Subqueries
Joining several has_many tables in ActiveRecord multiplies rows and inflates SUM and COUNT. Tested fixes with where(id: subquery) and EXISTS, with EXPLAIN times.
Practical guides for converting raw SQL queries into correct, performant ActiveRecord code, including edge cases where Rails behaves differently than SQL.
Joining several has_many tables in ActiveRecord multiplies rows and inflates SUM and COUNT. Tested fixes with where(id: subquery) and EXISTS, with EXPLAIN times.
The same SELECT running again and again in one Rails request is not an N+1, and Bullet does not report it. How to spot duplicate queries and remove them, tested.
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.
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.
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.
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.