aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-09-14 07:29:22 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-09-14 07:29:22 +0000
commit498d8ff72eded86fb1064205332e0a91b1dacece (patch)
tree6e7c90e4eeb6c1a2dc46ac6b6e26e8e1509e4bd5
parent12f8f8930794b2ae56630e846300c3aff0a7a58e (diff)
downloadrails-498d8ff72eded86fb1064205332e0a91b1dacece.tar.gz
rails-498d8ff72eded86fb1064205332e0a91b1dacece.tar.bz2
rails-498d8ff72eded86fb1064205332e0a91b1dacece.zip
Fix association writer with :dependent => :nullify. Closes #7314.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7477 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb19
-rwxr-xr-xactiverecord/test/associations_test.rb5
3 files changed, 18 insertions, 8 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 82095b1759..81cf47529e 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fix association writer with :dependent => :nullify. #7314 [Jonathan Viney]
+
* OpenBase: update for new lib and latest Rails. Support migrations. #8748 [dcsesq]
* Moved acts_as_tree into a plugin of the same name on the official Rails svn #9514 [lifofifo]
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index d385691967..d07d460b0a 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -125,14 +125,17 @@ module ActiveRecord
end
def delete_records(records)
- if @reflection.options[:dependent]
- records.each { |r| r.destroy }
- else
- ids = quoted_record_ids(records)
- @reflection.klass.update_all(
- "#{@reflection.primary_key_name} = NULL",
- "#{@reflection.primary_key_name} = #{@owner.quoted_id} AND #{@reflection.klass.primary_key} IN (#{ids})"
- )
+ case @reflection.options[:dependent]
+ when :destroy
+ records.each(&:destroy)
+ when :delete_all
+ @reflection.klass.delete(records.map(&:id))
+ else
+ ids = quoted_record_ids(records)
+ @reflection.klass.update_all(
+ "#{@reflection.primary_key_name} = NULL",
+ "#{@reflection.primary_key_name} = #{@owner.quoted_id} AND #{@reflection.klass.primary_key} IN (#{ids})"
+ )
end
end
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 6b97a92612..a1ea3f1c23 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -961,6 +961,11 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert_equal 1, firm.clients.length
end
+ def test_replace_with_less_and_dependent_nullify
+ num_companies = Company.count
+ companies(:rails_core).companies = []
+ assert_equal num_companies, Company.count
+ end
def test_replace_with_new
firm = Firm.find(:first)