aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2005-10-13 19:21:45 +0000
committerJamis Buck <jamis@37signals.com>2005-10-13 19:21:45 +0000
commit99f3ae08450ecfd7574336837a75c7fd0dd17305 (patch)
tree772950a2a833e41301f1e410f7a2d1fc21b35ff6 /activerecord
parent2f9a19cb834b5d44ccb28b87b979266820864dd1 (diff)
downloadrails-99f3ae08450ecfd7574336837a75c7fd0dd17305.tar.gz
rails-99f3ae08450ecfd7574336837a75c7fd0dd17305.tar.bz2
rails-99f3ae08450ecfd7574336837a75c7fd0dd17305.zip
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
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_association.rb2
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb4
-rwxr-xr-xactiverecord/test/associations_test.rb10
4 files changed, 15 insertions, 3 deletions
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) }