diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-02-01 22:56:04 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-02-07 23:35:05 +0000 |
commit | 05bcb8cecc8573f28ad080839233b4bb9ace07be (patch) | |
tree | a5c2f2f164081c64b923eb1fa108d38b78b8d145 /activerecord/lib/active_record/associations/has_many_through_association.rb | |
parent | d55406d2e991056b08f69eb68bcf9b17da807b6c (diff) | |
download | rails-05bcb8cecc8573f28ad080839233b4bb9ace07be.tar.gz rails-05bcb8cecc8573f28ad080839233b4bb9ace07be.tar.bz2 rails-05bcb8cecc8573f28ad080839233b4bb9ace07be.zip |
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.
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, 8 insertions, 9 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 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 |