From 2563708b8b8c1cc570a627a7d8df2872a407b251 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 22 Nov 2010 14:23:34 -0800 Subject: [PATCH] fix has_one :through belongs_to associations for delted records --- .../associations/has_one_through_association.rb | 4 +++ .../has_one_through_associations_test.rb | 24 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index 6e98f7d..c482dd1 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -32,6 +32,10 @@ module ActiveRecord end private + def foreign_key_present + reflection = @reflection.through_reflection + reflection.belongs_to? && !@owner[reflection.primary_key_name].nil? + end def find_target with_scope(@scope) { @reflection.klass.find(:first) } end diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index 5d15314..c3d8619 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -206,6 +206,30 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase end end + def test_through_belongs_to_after_destroy + @member_detail = MemberDetail.new(:extra_data => 'Extra') + @member.member_detail = @member_detail + @member.save! + + assert_not_nil @member_detail.member_type + @member_detail.destroy + assert_queries(1) do + assert_not_nil @member_detail.member_type(true) + end + + @member_detail.member.destroy + assert_queries(1) do + assert_nil @member_detail.member_type(true) + end + end + + def test_through_has_one_after_destroy + @member.destroy + assert_no_queries do + assert_nil @member.club + end + end + def test_value_is_properly_quoted minivan = Minivan.find('m1') assert_nothing_raised do -- 1.7.3.2