Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix errors caused by assigning a has-one or belongs-to property to it…

…self

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2562 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 99f3ae08450ecfd7574336837a75c7fd0dd17305 1 parent 2f9a19c
@jamis jamis authored
View
2  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]
View
2  activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -30,7 +30,7 @@ def replace(obj, dont_save = false)
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
View
4 activerecord/lib/active_record/associations/has_one_association.rb
@@ -29,7 +29,7 @@ def build(attributes = {}, replace_existing = true)
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 @@ def replace(obj, dont_save = false)
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
View
10 activerecord/test/associations_test.rb
@@ -72,6 +72,11 @@ def test_has_one
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 @@ def test_belongs_to
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) }
Please sign in to comment.
Something went wrong with that request. Please try again.