SQL to ActiveRecord Converters
Pick the SQL you are translating. Each page shows the ActiveRecord code, the SQL Rails really generates, and the mistakes that change your results. Examples were run on Rails 8.1.1 and PostgreSQL 16.
By SQL feature
- Rails LEFT JOIN with ActiveRecord left_joins Convert a SQL LEFT JOIN to Rails ActiveRecord with left_joins and where.missing. Tested on Rails 8.1 and PostgreSQL 16, with the generated SQL shown.
- Rails GROUP BY HAVING in ActiveRecord Convert SQL GROUP BY with HAVING to Rails ActiveRecord using group, having and count. Tested on Rails 8.1 with the generated SQL and the Hash it returns.
- ActiveRecord Subquery in Rails Convert a SQL IN (SELECT ...) subquery to Rails ActiveRecord with where(id: relation.select). Tested on Rails 8.1, with the NOT IN NULL trap explained.
- Rails WHERE EXISTS in ActiveRecord Convert SQL WHERE EXISTS to Rails ActiveRecord with arel.exists. Tested on Rails 8.1 and PostgreSQL 16, with joins and where.associated compared.
- Rails CASE WHEN in ActiveRecord Convert SQL CASE WHEN to Rails ActiveRecord with select, Arel.sql and Arel::Nodes::Case. Tested on Rails 8.1, including the order and pluck errors.
- Rails WHERE IN Clause in ActiveRecord Convert SQL IN and NOT IN to Rails ActiveRecord with where(id: [..]) and where.not. Tested on Rails 8.1, including empty arrays and NULL values.
- Rails ILIKE Query in ActiveRecord Convert SQL ILIKE to Rails ActiveRecord with a bound string or Arel matches. Tested on Rails 8.1 and PostgreSQL 16, including escaping percent signs.
- Rails DISTINCT ON in ActiveRecord (PostgreSQL) Convert PostgreSQL DISTINCT ON to Rails ActiveRecord with select and order. Tested on Rails 8.1, including why count raises a syntax error.
- Rails CTE (WITH) in ActiveRecord Convert a SQL WITH clause to Rails ActiveRecord using Relation#with and from. Tested on Rails 8.1 and PostgreSQL 16. Needs Rails 7.1.
- Rails Upsert (ON CONFLICT) in ActiveRecord Convert INSERT ON CONFLICT DO UPDATE to Rails with upsert and unique_by. Tested on Rails 8.1 and PostgreSQL 16, with callbacks explained.
- Rails Window Function in ActiveRecord Convert SQL window functions like ROW_NUMBER OVER PARTITION BY to Rails ActiveRecord. Tested on Rails 8.1, including how to filter on the window result.
- Rails UNION Query in ActiveRecord Rails has no built-in union method. Build UNION and UNION ALL in ActiveRecord with Arel, tested on Rails 8.1 and PostgreSQL 16, plus when .or is enough.
- Rails N+1 Query Fix: includes and strict_loading Fix N+1 queries in Rails with includes, preload and strict_loading. Query counts measured on Rails 8.1 and PostgreSQL 16: 10 queries drop to 2.
- Rails includes vs preload vs eager_load includes, preload and eager_load compared with the real SQL from Rails 8.1 and PostgreSQL 16. See which runs 2 queries, which runs a JOIN, and which fails.
- Rails joins vs includes Rails joins vs includes with measured query counts and real SQL from Rails 8.1 and PostgreSQL 16. joins filters, includes loads, and joins does not fix N+1.
- ActiveRecord explain: Read the Query Plan Run EXPLAIN and EXPLAIN ANALYZE from ActiveRecord with relation.explain. Real PostgreSQL 16 plan output, options like :buffers, and how to read Seq Scan.
- Find Slow Queries in Rails with PostgreSQL Find slow queries in a Rails app on PostgreSQL: subscribe to sql.active_record, set log_min_duration_statement, then run explain(:analyze) on the worst one.
- Rails find_each vs each find_each loads records in batches of 1000 by primary key, each loads every row at once. Tested on Rails 8.1: ignored order, start and finish, and cursor.