aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-11-02 09:45:17 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-11-02 09:45:17 +0000
commitac715b2bcbc1dee525339edec86362261e017bbe (patch)
tree4822bf089421a5f68d4b1f9e6fa069a0d8723da9
parenteb7964865a5083615503896f7793051491eae23d (diff)
downloadrails-ac715b2bcbc1dee525339edec86362261e017bbe.tar.gz
rails-ac715b2bcbc1dee525339edec86362261e017bbe.tar.bz2
rails-ac715b2bcbc1dee525339edec86362261e017bbe.zip
has_one :dependent => :nullify ignores nil associates. Closes #6528.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5401 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb2
-rwxr-xr-xactiverecord/test/associations_test.rb12
3 files changed, 14 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index db50f823ae..0f7af0d471 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* has_one :dependent => :nullify ignores nil associates. #6528 [janovetz, Jeremy Kemper]
+
* Oracle: resolve test failures, use prefetched primary key for inserts, check for null defaults. Factor out some common methods from all adapters. #6515 [Michael Schoen]
* Make add_column use the options hash with the Sqlite Adapter. Closes #6464 [obrie]
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index f40e44e804..8a1b6ca933 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1060,7 +1060,7 @@ module ActiveRecord
when :delete
module_eval "before_destroy '#{reflection.class_name}.delete(#{reflection.name}.id) unless #{reflection.name}.nil?'"
when :nullify
- module_eval "before_destroy '#{reflection.name}.update_attribute(\"#{reflection.primary_key_name}\", nil)'"
+ module_eval "before_destroy '#{reflection.name}.update_attribute(\"#{reflection.primary_key_name}\", nil) unless #{reflection.name}.nil?'"
when nil, false
# pass
else
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 0be63fccbf..f1b02cfda5 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -184,11 +184,13 @@ class HasOneAssociationsTest < Test::Unit::TestCase
def test_dependence
num_accounts = Account.count
+
firm = Firm.find(1)
assert !firm.account.nil?
account_id = firm.account.id
assert_equal [], Account.destroyed_account_ids[firm.id]
- firm.destroy
+
+ firm.destroy
assert_equal num_accounts - 1, Account.count
assert_equal [account_id], Account.destroyed_account_ids[firm.id]
end
@@ -201,15 +203,23 @@ class HasOneAssociationsTest < Test::Unit::TestCase
def test_exclusive_dependence
num_accounts = Account.count
+
firm = ExclusivelyDependentFirm.find(9)
assert !firm.account.nil?
account_id = firm.account.id
assert_equal [], Account.destroyed_account_ids[firm.id]
+
firm.destroy
assert_equal num_accounts - 1, Account.count
assert_equal [], Account.destroyed_account_ids[firm.id]
end
+ def test_dependence_with_nil_associate
+ firm = DependentFirm.new(:name => 'nullify')
+ firm.save!
+ assert_nothing_raised { firm.destroy }
+ end
+
def test_succesful_build_association
firm = Firm.new("name" => "GlobalMegaCorp")
firm.save