From a3fcf4cefd7d7f676ecbba82e87315f94667dcff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Wn=C4=99trzak?= Date: Thu, 16 Dec 2010 10:22:00 +0100 Subject: [PATCH] [#6150] added failing test where associated object is nullified to early --- .../associations/has_one_associations_test.rb | 7 +++++++ activerecord/test/models/company.rb | 6 ++++++ 2 files changed, 13 insertions(+), 0 deletions(-) diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index c1dad5e..a8bc3ba 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -359,4 +359,11 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_equal pirate.id, ships(:black_pearl).reload.pirate_id assert_nil new_ship.pirate_id end + + def test_has_one_relation_not_nullifies_associated_object_too_early + dependent_firm = companies(:rails_core) + account = accounts(:rails_core_account) + dependent_firm.destroy + assert_not_nil account.reload.firm_name + end end diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index e0b30ef..9e21b50 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -104,6 +104,7 @@ end class DependentFirm < Company has_one :account, :foreign_key => "firm_id", :dependent => :nullify has_many :companies, :foreign_key => 'client_of', :dependent => :nullify + before_destroy { |company| company.account && company.account.set_dependent_firm_name } end class RestrictedFirm < Company @@ -177,6 +178,11 @@ end class Account < ActiveRecord::Base belongs_to :firm, :class_name => 'Company' belongs_to :unautosaved_firm, :foreign_key => "firm_id", :class_name => "Firm", :autosave => false + belongs_to :dependent_firm, :foreign_key => "firm_id" + + def set_dependent_firm_name + self.firm_name = dependent_firm.try(:name) + end def self.destroyed_account_ids @destroyed_account_ids ||= Hash.new { |h,k| h[k] = [] } -- 1.7.1.1