ActiveRecord where vs find_by — Which to Use When
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.
Core language and framework features, Rails 8.1 updates, Ruby 3.4 improvements
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.
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.
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.
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.