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.
Practical guides for converting raw SQL queries into correct, performant ActiveRecord code, including edge cases where Rails behaves differently than SQL.
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.
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.
Convert SQL SELECT DISTINCT to ActiveRecord: distinct, pluck, DISTINCT ON and the PostgreSQL ORDER BY error, with tested Rails 8.1 examples.
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.
Rails query ignoring your PostgreSQL index? Six ActiveRecord patterns cause it: LOWER(), OR, joins, NOT, leading LIKE wildcards. Spot each with EXPLAIN.