Skip to content
This repository
Browse code

Fix that has_one natural assignment to already associated record. [#854

… state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit ea40f71431a821b2ddb37be6ea3ee7d8dac63b85 1 parent 7e4ea5f
authored August 21, 2008 lifo committed August 21, 2008
4  activerecord/lib/active_record/associations/has_one_association.rb
@@ -21,8 +21,8 @@ def build(attrs = {}, replace_existing = true)
21 21
       def replace(obj, dont_save = false)
22 22
         load_target
23 23
 
24  
-        unless @target.nil?
25  
-          if dependent? && !dont_save && @target != obj
  24
+        unless @target.nil? || @target == obj
  25
+          if dependent? && !dont_save
26 26
             @target.destroy unless @target.new_record?
27 27
             @owner.clear_association_cache
28 28
           else
10  activerecord/test/cases/associations/has_one_associations_test.rb
@@ -79,6 +79,16 @@ def test_natural_assignment_to_nil
79 79
     assert_raises(ActiveRecord::RecordNotFound) { Account.find(old_account_id) }
80 80
   end
81 81
 
  82
+  def test_natural_assignment_to_already_associated_record
  83
+    company = companies(:first_firm)
  84
+    account = accounts(:signals37)
  85
+    assert_equal company.account, account
  86
+    company.account = account
  87
+    company.reload
  88
+    account.reload
  89
+    assert_equal company.account, account
  90
+  end
  91
+
82 92
   def test_assignment_without_replacement
83 93
     apple = Firm.create("name" => "Apple")
84 94
     citibank = Account.create("credit_limit" => 10)

1 note on commit ea40f71

Jacek Becela

Congrats Jakub, waiting for more :)

Please sign in to comment.
Something went wrong with that request. Please try again.