From 99f3ae08450ecfd7574336837a75c7fd0dd17305 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Thu, 13 Oct 2005 19:21:45 +0000 Subject: Fix errors caused by assigning a has-one or belongs-to property to itself git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2562 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ .../lib/active_record/associations/belongs_to_association.rb | 2 +- .../lib/active_record/associations/has_one_association.rb | 4 ++-- activerecord/test/associations_test.rb | 10 ++++++++++ 4 files changed, 15 insertions(+), 3 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 8aa05e88ce..5024362bf5 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fix errors caused by assigning a has-one or belongs-to property to itself + * Add ActiveRecord::Base.schema_format setting which specifies how databases should be dumped [Sam Stephenson] * Optimize postgresql selects. [skaes@web.de] diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 681bd1b88d..9dc0e26e10 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -30,7 +30,7 @@ module ActiveRecord else raise_on_type_mismatch(obj) unless obj.nil? - @target = obj + @target = (AssociationProxy === obj ? obj.target : obj) @owner[@association_class_primary_key_name] = obj.id unless obj.new_record? @updated = true end diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 1550db69b5..1ff67ff70a 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -29,7 +29,7 @@ module ActiveRecord def replace(obj, dont_save = false) load_target unless @target.nil? - if dependent? && !dont_save + if dependent? && !dont_save && @target != obj @target.destroy unless @target.new_record? @owner.clear_association_cache else @@ -44,7 +44,7 @@ module ActiveRecord raise_on_type_mismatch(obj) obj[@association_class_primary_key_name] = @owner.id unless @owner.new_record? - @target = obj + @target = (AssociationProxy === obj ? obj.target : obj) end @loaded = true diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index af4251284d..fc8d59e862 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -72,6 +72,11 @@ class HasOneAssociationsTest < Test::Unit::TestCase assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit end + def test_proxy_assignment + company = companies(:first_firm) + assert_nothing_raised { company.account = company.account } + end + def test_triple_equality assert Account === companies(:first_firm).account end @@ -692,6 +697,11 @@ class BelongsToAssociationsTest < Test::Unit::TestCase assert !Client.find(3).firm.nil?, "Microsoft should have a firm" end + def test_proxy_assignment + account = Account.find(1) + assert_nothing_raised { account.firm = account.firm } + end + def test_type_mismatch assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = 1 } assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = Project.find(1) } -- cgit v1.2.3