From e5bcc3b32b4c651d5d48eb3338eaf2308197dbf9 Mon Sep 17 00:00:00 2001 From: Tanel Suurhans Date: Fri, 30 Apr 2010 15:57:25 +0300 Subject: [PATCH] Proposed fix for issue related and described in ticket #974 --- activerecord/lib/active_record/associations.rb | 12 +++++++----- .../associations/has_one_associations_test.rb | 14 ++++++++++++++ activerecord/test/models/member.rb | 1 + 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 6c64210..8a687b1 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1561,12 +1561,14 @@ module ActiveRecord end eoruby when :nullify - class_eval <<-eoruby, __FILE__, __LINE__ + 1 - def #{method_name} - association = #{reflection.name} - association.update_attribute(#{reflection.primary_key_name.inspect}, nil) if association + define_method(method_name) do + association = send(reflection.name) + if reflection.options.has_key?(:as) + association.update_attribute(reflection.options[:as], nil) if association + else + association.update_attribute(reflection.primary_key_name, nil) if association end - eoruby + end when :restrict method_name = "has_one_dependent_restrict_for_#{reflection.name}".to_sym define_method(method_name) do diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 8f55409..02ab0a6 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/member' +require 'models/sponsor' class HasOneAssociationsTest < ActiveRecord::TestCase fixtures :accounts, :companies, :developers, :projects, :developers_projects @@ -326,4 +328,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