diff options
author | Piotr Jakubowski <piotrj@gmail.com> | 2016-01-20 21:53:17 +0100 |
---|---|---|
committer | Piotr Jakubowski <piotrj@gmail.com> | 2016-05-04 22:01:24 +0200 |
commit | eebcebdeb58ff7b6c05cb1cfbbc9aa4c85c9800e (patch) | |
tree | 2b7f9fb90a4171432ddd7751fd1599eeca930d15 /activerecord/lib/active_record/associations/has_many_through_association.rb | |
parent | 8352c8c6ef58eef822947faf8defc378e5de057c (diff) | |
download | rails-eebcebdeb58ff7b6c05cb1cfbbc9aa4c85c9800e.tar.gz rails-eebcebdeb58ff7b6c05cb1cfbbc9aa4c85c9800e.tar.bz2 rails-eebcebdeb58ff7b6c05cb1cfbbc9aa4c85c9800e.zip |
When deleting through records, take into account association conditions
Fixes #18424.
When deleting through records, it didn't take into account the
conditions that may have been affecting join model table, but was
defined in association definition.
Diffstat (limited to 'activerecord/lib/active_record/associations/has_many_through_association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 36fc381343..5182a9b39d 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -132,26 +132,27 @@ module ActiveRecord scope = through_association.scope scope.where! construct_join_attributes(*records) + scope_with_association_conditions = scope.where through_scope_attributes case method when :destroy - if scope.klass.primary_key - count = scope.destroy_all.length + if scope_with_association_conditions.klass.primary_key + count = scope_with_association_conditions.destroy_all.length else - scope.each(&:_run_destroy_callbacks) + scope_with_association_conditions.each(&:_run_destroy_callbacks) - arel = scope.arel + arel = scope_with_association_conditions.arel stmt = Arel::DeleteManager.new - stmt.from scope.klass.arel_table + stmt.from scope_with_association_conditions.klass.arel_table stmt.wheres = arel.constraints - count = scope.klass.connection.delete(stmt, 'SQL', scope.bound_attributes) + count = scope_with_association_conditions.klass.connection.delete(stmt, 'SQL', scope_with_association_conditions.bound_attributes) end when :nullify - count = scope.update_all(source_reflection.foreign_key => nil) + count = scope_with_association_conditions.update_all(source_reflection.foreign_key => nil) else - count = scope.delete_all + count = scope_with_association_conditions.delete_all end delete_through_records(records) |