diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-10-16 01:33:40 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-11-06 04:04:54 +0900 |
commit | 8a2ee3d8c6921ce34e597658e3f2b43b41423d1d (patch) | |
tree | 4e9a337567e3de1b18901b4754a589652451c1ba /activerecord/lib | |
parent | e3a295664681ea0480a9c95f6bfc776a113ff926 (diff) | |
download | rails-8a2ee3d8c6921ce34e597658e3f2b43b41423d1d.tar.gz rails-8a2ee3d8c6921ce34e597658e3f2b43b41423d1d.tar.bz2 rails-8a2ee3d8c6921ce34e597658e3f2b43b41423d1d.zip |
Ensure `apply_join_dependency` for `update_all` and `delete_all` if eager-loading is needed
If a relation has eager-loading values, `count` and `exists?` works
properly, but `update_all` and `delete_all` doesn't work due to missing
`apply_join_dependency`. It should be applied to work consistently.
Fixes #28863.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 997cfe4b5e..7615fb6ee9 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -359,6 +359,11 @@ module ActiveRecord def update_all(updates) raise ArgumentError, "Empty list of attributes to change" if updates.blank? + if eager_loading? + relation = apply_join_dependency + return relation.update_all(updates) + end + stmt = Arel::UpdateManager.new stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates)) @@ -423,6 +428,11 @@ module ActiveRecord raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}") end + if eager_loading? + relation = apply_join_dependency + return relation.delete_all + end + stmt = Arel::DeleteManager.new stmt.from(table) |