diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-27 18:51:32 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-10-27 18:51:32 +0000 |
commit | 4b639904d7bc4407657767bb963997bc78678d03 (patch) | |
tree | 8819aed22cbcd90b7decee02639a275adbf01572 /activerecord/lib/active_record/associations/has_many_through_association.rb | |
parent | cf9be897b81b5cda0282fb3439c11af6032ecedb (diff) | |
download | rails-4b639904d7bc4407657767bb963997bc78678d03.tar.gz rails-4b639904d7bc4407657767bb963997bc78678d03.tar.bz2 rails-4b639904d7bc4407657767bb963997bc78678d03.zip |
Fix has_many :through delete with custom foreign keys. Closes #6466.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8043 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
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 | 22 |
1 files changed, 14 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 5bb306c060..5ff0e3d0d7 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -71,18 +71,24 @@ module ActiveRecord def delete(*records) records = flatten_deeper(records) records.each { |associate| raise_on_type_mismatch(associate) } - records.reject! { |associate| @target.delete(associate) if associate.new_record? } - return if records.empty? - - @delete_join_finder ||= "find_all_by_#{@reflection.source_reflection.association_foreign_key}" + through = @reflection.through_reflection - through.klass.transaction do - records.each do |associate| - joins = @owner.send(through.name).send(@delete_join_finder, associate.id) - @owner.send(through.name).delete(joins) + raise ActiveRecord::HasManyThroughCantDissociateNewRecords.new(@owner, through) if @owner.new_record? + + load_target + + klass = through.klass + klass.transaction do + flatten_deeper(records).each do |associate| + raise_on_type_mismatch(associate) + raise ActiveRecord::HasManyThroughCantDissociateNewRecords.new(@owner, through) unless associate.respond_to?(:new_record?) && !associate.new_record? + + @owner.send(through.name).proxy_target.delete(klass.delete_all(construct_join_attributes(associate))) @target.delete(associate) end end + + self end def build(attrs = nil) |