Rails LEFT JOIN with ActiveRecord left_joins
Use left_joins to turn a SQL LEFT JOIN into ActiveRecord. The example finds users who have no posts, the most common reason to write a LEFT JOIN. Rails emits LEFT OUTER JOIN, which is the same join PostgreSQL runs for LEFT JOIN.
Tested on Rails 8.1.1 and PostgreSQL 16, . Every example below ran, and the generated SQL is the SQL Rails sent.
The conversion
SQL
SELECT users.* FROM users
LEFT JOIN posts ON posts.user_id = users.id
WHERE posts.id IS NULL
ActiveRecord
User.left_joins(:posts).where(posts: { id: nil })
SQL that Rails 8.1.1 generates
SELECT "users".* FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id" WHERE "posts"."id" IS NULL
A cleaner alternative
User.where.missing(:posts)
where.missing (Rails 6.1 and later) generates the same LEFT OUTER JOIN with posts.id IS NULL. Prefer it when the goal is rows with no match. It reads as intent.
What goes wrong
A WHERE on the joined table can cancel the LEFT JOIN
Filtering the joined table with a real value, for example where(posts: { published: true }), removes the NULL rows the LEFT JOIN kept. The SQL still says LEFT OUTER JOIN, but the result matches an INNER JOIN. To keep the NULL rows, declare a scoped association such as has_many :published_posts, -> { where(published: true) }, class_name: "Post" and call left_joins(:published_posts). Rails puts that condition in the ON clause.
joins is INNER JOIN, left_joins is LEFT OUTER JOIN
User.joins(:posts) drops users without posts. Swapping joins for left_joins is the whole conversion for most queries.
left_joins does not load the association
It only adds the JOIN. Calling user.posts afterward still runs one query per user. Add includes or preload if you also read the posts, and compare with eager_load if you need to filter on them.
Convert your own LEFT JOIN query
Paste your SQL and press Convert. Always run .to_sql on the result and compare it with your original.
Questions
How do I write a LEFT JOIN in Rails ActiveRecord?
Call left_joins with an association name, for example User.left_joins(:posts). Rails generates LEFT OUTER JOIN posts ON posts.user_id = users.id.
What is the difference between left_joins and includes?
left_joins only adds the JOIN to the SQL and loads no associated records. includes loads the associated records to prevent N+1 queries and picks its own strategy.
How do I find records with no associated records?
Use User.where.missing(:posts) on Rails 6.1 or later. It produces a LEFT OUTER JOIN with WHERE posts.id IS NULL.
Related conversions
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.
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 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.
All SQL converters
Every tested SQL to ActiveRecord conversion on this site.
Safe conversion guide
How to check that a converted query returns the same rows.