From 05bcb8cecc8573f28ad080839233b4bb9ace07be Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 1 Feb 2011 22:56:04 +0000 Subject: Support the :dependent option on has_many :through associations. For historical and practical reasons, :delete_all is the default deletion strategy employed by association.delete(*records), despite the fact that the default strategy is :nullify for regular has_many. Also, this only works at all if the source reflection is a belongs_to. For other situations, you should directly modify the through association. --- .../associations/has_many_through_association.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 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 3174ea6373..c98ac79dc0 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -43,19 +43,18 @@ module ActiveRecord end end - # TODO - add dependent option support - def delete_records(records, method = @reflection.options[:dependent]) - through_association = @owner.send(@reflection.through_reflection.name) + def deletion_scope(records) + @owner.send(@reflection.through_reflection.name).where(construct_join_attributes(*records)) + end + def delete_records(records, method = @reflection.options[:dependent]) case method when :destroy - records.each do |record| - through_association.where(construct_join_attributes(record)).destroy_all - end + deletion_scope(records).destroy_all + when :nullify + deletion_scope(records).update_all(@reflection.source_reflection.foreign_key => nil) else - records.each do |record| - through_association.where(construct_join_attributes(record)).delete_all - end + deletion_scope(records).delete_all end end -- cgit v1.2.3