aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew White <andyw@pixeltrix.co.uk>2011-05-11 14:22:22 +0100
committerAndrew White <andyw@pixeltrix.co.uk>2011-05-11 14:22:22 +0100
commit0fde84c0f8c0db40110c27890525221751015e6d (patch)
treee1d5c5090118bf832a20750ad8226ec483fe5f58
parentb3f45195aa8a35277c3f998917312797936a1f4e (diff)
downloadrails-0fde84c0f8c0db40110c27890525221751015e6d.tar.gz
rails-0fde84c0f8c0db40110c27890525221751015e6d.tar.bz2
rails-0fde84c0f8c0db40110c27890525221751015e6d.zip
Don't remove the target if it has already been destroyed
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb2
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb9
2 files changed, 10 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index 7134dc85c8..2f3a6e71f1 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -10,7 +10,7 @@ module ActiveRecord
reflection.klass.transaction do
if target && target != record
- remove_target!(options[:dependent])
+ remove_target!(options[:dependent]) unless target.destroyed?
end
if record
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index 625515f4f1..c2a82b8c60 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -95,6 +95,15 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_nil Account.find(old_account_id).firm_id
end
+ def test_natural_assignment_to_nil_after_destroy
+ firm = companies(:rails_core)
+ old_account_id = firm.account.id
+ firm.account.destroy
+ firm.account = nil
+ assert_nil companies(:rails_core).account
+ assert_raise(ActiveRecord::RecordNotFound) { Account.find(old_account_id) }
+ end
+
def test_association_change_calls_delete
companies(:first_firm).deletable_account = Account.new(:credit_limit => 5)
assert_equal [], Account.destroyed_account_ids[companies(:first_firm).id]