From eebcebdeb58ff7b6c05cb1cfbbc9aa4c85c9800e Mon Sep 17 00:00:00 2001 From: Piotr Jakubowski Date: Wed, 20 Jan 2016 21:53:17 +0100 Subject: 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. --- .../associations/has_many_through_association.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record/associations/has_many_through_association.rb') 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) -- cgit v1.2.3