DELETE with JOIN in ActiveRecord
Delete with a join in ActiveRecord: tested SQL for delete_all, where.missing and DELETE USING on PostgreSQL, plus skipped callbacks.
A curated library of common SQL patterns and their correct ActiveRecord equivalents, serving as a reference for real-world Rails queries.
Delete with a join in ActiveRecord: tested SQL for delete_all, where.missing and DELETE USING on PostgreSQL, plus skipped callbacks.
LOWER() in ActiveRecord where and order, why a plain index is ignored, and the add_index expression fix, with EXPLAIN on PostgreSQL.
Keyset vs OFFSET pagination in ActiveRecord with tested SQL, EXPLAIN output, tie-breaker columns, and the limits you accept.
UPDATE with JOIN in Rails on PostgreSQL: joins plus update_all, the UPDATE FROM SQL on 8.1.1, the 8.0 error and safe alternatives. Tested.
ActiveRecord has no intersect or except method. Four tested options for Rails 8.1: Arel nodes, from with raw SQL, find_by_sql and where IN rewrites.
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.
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.
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.
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.
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.
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.