From fc457747fb679dc6fe0a0096e079c1a54361a6a0 Mon Sep 17 00:00:00 2001 From: Tanel Suurhans Date: Fri, 30 Apr 2010 18:08:34 +0300 Subject: [PATCH] Proposed fix for the original issue described by Brad Gessler in ticket #974 --- .../associations/has_one_association.rb | 4 +- .../associations/has_one_associations_test.rb | 27 +++++++++++++++++++- activerecord/test/models/member.rb | 1 + 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index ea769fd..b4df29c 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -40,11 +40,11 @@ module ActiveRecord @target.destroy unless @target.new_record? @owner.clear_association_cache when :nullify - @target[@reflection.primary_key_name] = nil + @target.send("#{@reflection.options.fetch(:as, @reflection.primary_key_name)}=", nil) @target.save unless @owner.new_record? || @target.new_record? end else - @target[@reflection.primary_key_name] = nil + @target.send("#{@reflection.options.fetch(:as, @reflection.primary_key_name)}=", nil) @target.save unless @owner.new_record? || @target.new_record? end end diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 8f55409..4d4ffa8 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 @@ -326,4 +328,27 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert !account.new_record? assert_equal 500, account.credit_limit end -end + + def test_foreign_type_on_nil_assign_for_polymorphic_has_one_with + sponsor = Sponsor.create + supporter = Sponsor.create + + member = Member.new + member.supporter = supporter + member.sponsor = sponsor + member.save + + member.supporter = nil + member.sponsor = nil + member.save + + sponsor.reload + supporter.reload + + assert_nil sponsor.sponsorable_id + assert_nil sponsor.sponsorable_type + + assert_nil supporter.sponsorable_id + assert_nil supporter.sponsorable_type + end +end \ No newline at end of file diff --git a/activerecord/test/models/member.rb b/activerecord/test/models/member.rb index 255fb56..e32e420 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, :as => :sponsorable, :class_name => "Sponsor", :dependent => :nullify has_one :sponsor_club, :through => :sponsor has_one :member_detail has_one :organization, :through => :member_detail -- 1.7.0.5