DEV Community

Discussion on: Why is Rails ActiveRelation.update_all updating a different set of records?

Collapse
 
rhymes profile image
rhymes • Edited

You probably hit a limitation of ActiveRecord :-)

union is not part of ActiveRecord, which might be the reason why update_all is not able to work with it.

There's a lot of magic in it :D

PostgreSQL itself is able to update rows from unions if you project the same exact row types from both queries:

PracticalDeveloper_development> select count(*) from comments;
+---------+
| count   |
|---------|
| 30      |
+---------+
PracticalDeveloper_development> update comments set updated_at = now() where id in (select id from comments where id < 10 union select id from comments where id > 15);
UPDATE 24

In conclusion: I don't know what's going on 🧐

Collapse
 
andy profile image
Andy Zhao (he/him)

Haha yeah the magic is certainly mystifying me. Guess I'll put on my wizard robes and dive into the code.

Collapse
 
rhymes profile image
rhymes

Let us know if you find anything :D