UPDATE with JOIN in Rails: update_all and joins
How update_all with joins behaves on PostgreSQL, the SQL Rails 8.1.1 generates, and what fails on older versions.
On Rails 8.1.1, Project.joins(:client).update_all("client_name = clients.name") works on PostgreSQL. Rails rewrites it to UPDATE ... FROM. On Rails 8.0.2 and 7.2.2 the same call fails with missing FROM-clause entry for table "clients", because those versions move the join into a subquery and your SET clause cannot see it. Callbacks, validations and updated_at are skipped on every version.
Tested on ActiveRecord 8.1.1, plus 8.0.2 and 7.2.2 in a separate gem directory, against PostgreSQL 16.15 and Ruby 3.3.6. PostgreSQL only. I did not test 8.0.3 and later patch releases, or 8.1.0.
The goal in SQL
Copy the client name onto each project:
UPDATE projects
SET client_name = c.name
FROM clients c
WHERE c.id = projects.client_id;
PostgreSQL has no UPDATE ... JOIN. It uses FROM. In ActiveRecord you write joins and update_all, and Rails picks the SQL shape.
Test data: two clients (Acme is a VIP), three projects. Two projects have a client. One has client_id NULL.
Rails 8.1.1: joins plus update_all
Project.joins(:client).update_all("client_name = clients.name")
# => 2
UPDATE "projects" "__active_record_update_alias"
SET client_name = clients.name
FROM "projects" INNER JOIN "clients" ON "clients"."id" = "projects"."client_id"
WHERE "projects"."id" = "__active_record_update_alias"."id"
Result: client_name was ["Acme", "Bolt", nil]. The project without a client was left alone, and the return value 2 counts only matched rows.
Read the SQL closely. Rails does not write UPDATE projects ... FROM clients. It aliases the target as __active_record_update_alias, then joins projects to clients again inside FROM and ties the two by primary key. The SQL contains a self-join. It is correct, and a hand-written UPDATE ... FROM has no such extra join. The PostgreSQL UPDATE docs say not to repeat the target table in FROM unless you intend a self-join. Rails intends one, and ties it back by primary key.
I timed both shapes on 200,000 projects and 1,000 clients with EXPLAIN (ANALYZE) inside a rolled-back transaction, one run each. The Rails shape took 1340 ms and added a second hash join over projects (4 batches). The hand-written UPDATE ... FROM took 934 ms. One run on one machine is not a benchmark, but the plan difference is real. Run EXPLAIN on your own data.
A Saeloun write-up names Rails 8.1 as the release that added joins support in update_all for PostgreSQL and SQLite. My tests agree: 8.0.2 failed, 8.1.1 worked.
The decision sits in prepare_update_statement in lib/arel/visitors/postgresql.rb of the 8.1.1 gem. Rails uses the self-join form only when the statement has a primary key, has join sources, has no LIMIT, OFFSET or ORDER, and does not combine GROUP BY with HAVING. Otherwise it falls back to the subquery.
Rails 8.0.2 and 7.2.2: the failure
UPDATE "projects" SET client_name = clients.name
WHERE ("projects"."id") IN (
SELECT "projects"."id" FROM "projects"
INNER JOIN "clients" ON "clients"."id" = "projects"."client_id")
ActiveRecord::StatementInvalid: PG::UndefinedTable:
ERROR: missing FROM-clause entry for table "clients"
Both older versions produced identical SQL and identical results. The join lives only inside the subquery, so clients.name in SET points at nothing.
Filtering through the join still worked on all three versions. Here where(clients: { vip: true }) is the filter.
Project.joins(:client).where(clients: { vip: true }).update_all(status: "priority")
-- 8.0.2 and 7.2.2
UPDATE "projects" SET "status" = $1 WHERE ("projects"."id") IN (
SELECT "projects"."id" FROM "projects"
INNER JOIN "clients" ON "clients"."id" = "projects"."client_id"
WHERE "clients"."vip" = $2)
-- 8.1.1
UPDATE "projects" "__active_record_update_alias" SET "status" = $1
FROM "projects" INNER JOIN "clients" ON "clients"."id" = "projects"."client_id"
WHERE "clients"."vip" = $2 AND "projects"."id" = "__active_record_update_alias"."id"
One row updated in both cases. If you only filter by the joined table, you do not need the new behavior. You only need it to read a joined column in SET.
Options for every version
A correlated subquery in SET needs no join at all:
Project.where.not(client_id: nil).update_all(
"client_name = (SELECT clients.name FROM clients WHERE clients.id = projects.client_id)"
)
UPDATE "projects"
SET client_name = (SELECT clients.name FROM clients WHERE clients.id = projects.client_id)
WHERE "projects"."client_id" IS NOT NULL
It produced ["Acme", "Bolt", nil] on all three versions. Keep the where.not(client_id: nil). Without it, projects with no client would be set to NULL by the subquery.
Raw UPDATE ... FROM through the connection also worked on all three:
ActiveRecord::Base.connection.exec_update(
"UPDATE projects SET client_name = c.name FROM clients c WHERE c.id = projects.client_id"
)
# => 2
exec_update returns the affected row count. It gives you full control of the join and no alias trick. It also bypasses the query interface, so tenant scopes and soft-delete default_scope conditions are not applied. Write them out by hand.
When you only filter by a joined table, the subquery form is portable:
Project.where(id: Project.joins(:client).where(clients: { vip: true }).select(:id))
.update_all(status: "priority")
Anti-joins work the same way
Project.left_joins(:client).where(clients: { id: nil }).update_all(status: "orphaned")
UPDATE "projects" "__active_record_update_alias" SET "status" = $1
FROM "projects" LEFT OUTER JOIN "clients" ON "clients"."id" = "projects"."client_id"
WHERE "clients"."id" IS NULL AND "projects"."id" = "__active_record_update_alias"."id"
One row changed, the project with no client. On 8.0.2 and 7.2.2 the same call also updated that one row, through the subquery form, because SET status does not need the join.
What still fails
limit pulls Rails back to the subquery. With 8.1.1:
Project.joins(:client).limit(1).update_all(status: "limited")
UPDATE "projects" SET "status" = $1 WHERE ("projects"."id") IN (
SELECT "projects"."id" FROM "projects"
INNER JOIN "clients" ON "clients"."id" = "projects"."client_id" LIMIT $2)
It updated one row, because SET status does not need the join. Now put a joined column back in SET and add one of these clauses:
| Relation | 8.1.1 result |
|---|---|
joins(:client).limit(1) |
missing FROM-clause entry for table "clients" |
joins(:client).order(:id) |
same error |
joins(:client).group("projects.id").having("count(*) > 0") |
same error |
joins(:client).group("projects.id") |
worked, 2 rows, no GROUP BY in the SQL |
The last row is a trap. GROUP BY without HAVING was dropped from the generated UPDATE, so it did nothing. The Saeloun post lists GROUP BY as unsupported. My result differs only in that the clause is ignored instead of rejected. Do not rely on it. On 8.0.2 and 7.2.2 all four relations failed.
The hash form cannot name a joined column:
Project.joins(:client).update_all(clients: { name: "z" })
# TypeError: can't cast Hash
You write the SET as a string or an Arel.sql fragment. Do not interpolate values into that string. Pass an array and Rails quotes the values: update_all(["client_name = clients.name, status = ?", status]). I tested the array form with values containing a quote on 8.1.1, without a join. I did not test it together with the joined SET column.
What gets skipped
update_all runs one statement. No before_update callback printed in my test, and no validation ran. updated_at did not change: zero rows had updated_at > created_at afterwards. If you want it touched, add it to the string:
Project.joins(:client).update_all("status = 'y', updated_at = CURRENT_TIMESTAMP")
Two rows then showed a newer updated_at. For a large update this is the right trade. For a small one needing callbacks, load the records with find_each and call update. You will pay one UPDATE per row.
Which one to use
- Rails 8.1 or later, reading a joined column in
SET:joins(...).update_all("col = table.col"). Check the SQL in the log once. - Older Rails: the correlated subquery or raw
UPDATE ... FROM. - Only filtering by a joined table: any form. The
where(id: subquery)version reads clearest. - Joined column in
SETpluslimit,order, orgroupwithhaving: the correlated subquery or raw SQL, on every version. - Big tables: run
EXPLAINon the generated SQL and update in batches by id range.
To translate your own statement, try the SQL UPDATE to ActiveRecord converter. For the join part alone, use the SQL JOIN to ActiveRecord converter.
Sources: PostgreSQL 16 UPDATE, ActiveRecord::Relation#update_all, and lib/arel/visitors/postgresql.rb in the activerecord 8.1.1 gem.
Related: WHERE conditions in ActiveRecord, EXISTS and NOT EXISTS, SQL CASE WHEN in Rails
Frequently asked questions
- Can update_all use joins on PostgreSQL?
- Yes. On 8.1.1 Rails generates UPDATE ... FROM with a self-join on the primary key. On 8.0.2 and 7.2.2 it generates a WHERE id IN subquery.
- Why do I get missing FROM-clause entry for table?
- On the older versions tested the join exists only in a subquery, so a SET clause naming the joined table fails. Upgrade, use a correlated subquery, or use raw UPDATE FROM.
- Does update_all run callbacks or touch updated_at?
- No. In the test no before_update callback ran and updated_at did not change. Add updated_at = CURRENT_TIMESTAMP to the SET string.
- Does limit work with joins in update_all?
- Yes, but Rails falls back to the WHERE id IN subquery form, as the log showed on 8.1.1.
- How do I write UPDATE ... FROM directly?
- Use ActiveRecord::Base.connection.exec_update with the SQL string. It returns the affected row count and skips model scopes.
- Why does update_all with limit or order and a joined column still fail on 8.1.1?
- With LIMIT, OFFSET, ORDER, or GROUP BY plus HAVING, Rails 8.1.1 falls back to the WHERE id IN subquery, so the joined table is not visible in SET. Use a correlated subquery or raw SQL.
Was this article helpful?
Your feedback helps us improve our content
How We Verify Conversions
Every conversion shown on this site follows a strict verification process to ensure correctness:
- Compare results on same dataset — We run both SQL and ActiveRecord against identical test data and verify results match
-
Check generated SQL with
to_sql— We inspect the actual SQL Rails generates to catch semantic differences (INNER vs LEFT JOIN, WHERE vs ON, etc.) - Add regression tests for tricky cases — Edge cases like NOT EXISTS, anti-joins, and predicate placement are tested with multiple scenarios
- Tested on Rails 8.1.1 — All conversions verified on current Rails version to ensure compatibility
Last updated: December 01, 2026
Try These Queries in Our Converter
See the SQL examples from this article converted to ActiveRecord—and compare the SQL Rails actually generates.
Deep Dive into ActiveRecord
Raza Hussain
Full-stack developer specializing in Ruby on Rails, React, and modern JavaScript. 15+ years upgrading and maintaining production Rails apps. Led Rails 4/5 → 7 upgrades with 40% performance gains, migrated apps from Heroku to Render cutting costs by 35%, and built systems for StatusGator, CryptoZombies, and others. Available for Rails upgrades, performance work, and cloud migrations.
More on SQL to ActiveRecord
SQL INTERSECT and EXCEPT in Rails ActiveRecord
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.
date_trunc and Group by Month in ActiveRecord
Group by month in ActiveRecord with PostgreSQL date_trunc. Tested time zone handling, key types, missing months, and what groupdate adds.
SQL LIKE and ILIKE in Rails ActiveRecord
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.
Leave a Response
Responses (0)
No responses yet
Be the first to share your thoughts