From cea1bd622c73a68e0e47a4fdd727d52b7fba57f4 Mon Sep 17 00:00:00 2001 From: Tanel Suurhans Date: Fri, 30 Apr 2010 16:13:59 +0300 Subject: [PATCH] Proposed fix for issue related and described in ticket #974 --- activerecord/lib/active_record/associations.rb | 6 +++++- .../associations/has_one_associations_test.rb | 14 ++++++++++++++ activerecord/test/models/member.rb | 1 + 3 files changed, 20 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index fcc79b5..49ab459 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1481,7 +1481,11 @@ module ActiveRecord method_name = "has_one_dependent_nullify_for_#{reflection.name}".to_sym define_method(method_name) do association = send(reflection.name) - association.update_attribute(reflection.primary_key_name, nil) unless association.nil? + if reflection.options.has_key?(:as) + association.update_attribute(reflection.options[:as], nil) unless association.nil? + else + association.update_attribute(reflection.primary_key_name, nil) unless association.nil? + end end before_destroy method_name else diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 69954af..f527f58 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -2,6 +2,8 @@ require "cases/helper" require 'models/developer' require 'models/project' require 'models/company' +require 'models/sponsor' +require 'models/member' class HasOneAssociationsTest < ActiveRecord::TestCase fixtures :accounts, :companies, :developers, :projects, :developers_projects @@ -327,4 +329,16 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert !account.new_record? assert_equal 500, account.credit_limit end + + def test_nullify_foreign_type_on_destroy_for_polymorphic_has_one + sponsor = Sponsor.create + member = Member.new + member.supporter = sponsor + member.save + member.destroy + + assert_nil sponsor.sponsorable + assert_nil sponsor.sponsorable_id + assert_nil sponsor.sponsorable_type + end end diff --git a/activerecord/test/models/member.rb b/activerecord/test/models/member.rb index 255fb56..34c2c4a 100644 --- a/activerecord/test/models/member.rb +++ b/activerecord/test/models/member.rb @@ -5,6 +5,7 @@ class Member < ActiveRecord::Base has_one :club, :through => :current_membership has_one :favourite_club, :through => :memberships, :conditions => ["memberships.favourite = ?", true], :source => :club has_one :sponsor, :as => :sponsorable + has_one :supporter, :class_name => "Sponsor", :as => :sponsorable, :dependent => :nullify has_one :sponsor_club, :through => :sponsor has_one :member_detail has_one :organization, :through => :member_detail -- 1.7.0.5