aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJakub Kuźma <kuba@ibm.(none)>2008-08-21 12:55:35 +0200
committerPratik Naik <pratiknaik@gmail.com>2008-08-21 15:21:21 +0100
commitea40f71431a821b2ddb37be6ea3ee7d8dac63b85 (patch)
tree2a56eb3d76b190d19795fd7f17ca48bc29ba15a0 /activerecord
parent7e4ea5f4a2fd25e06820689688e3db5a4851f8e0 (diff)
downloadrails-ea40f71431a821b2ddb37be6ea3ee7d8dac63b85.tar.gz
rails-ea40f71431a821b2ddb37be6ea3ee7d8dac63b85.tar.bz2
rails-ea40f71431a821b2ddb37be6ea3ee7d8dac63b85.zip
Fix that has_one natural assignment to already associated record. [#854 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb4
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb10
2 files changed, 12 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index fdc0fa52c9..18733255d2 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -21,8 +21,8 @@ module ActiveRecord
def replace(obj, dont_save = false)
load_target
- unless @target.nil?
- if dependent? && !dont_save && @target != obj
+ unless @target.nil? || @target == obj
+ if dependent? && !dont_save
@target.destroy unless @target.new_record?
@owner.clear_association_cache
else
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index 99639849a5..ec06be5eba 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -79,6 +79,16 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_raises(ActiveRecord::RecordNotFound) { Account.find(old_account_id) }
end
+ def test_natural_assignment_to_already_associated_record
+ company = companies(:first_firm)
+ account = accounts(:signals37)
+ assert_equal company.account, account
+ company.account = account
+ company.reload
+ account.reload
+ assert_equal company.account, account
+ end
+
def test_assignment_without_replacement
apple = Firm.create("name" => "Apple")
citibank = Account.create("credit_limit" => 10)