public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix that has_one natural assignment to already associated record. [#854 
state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Jakub Kuźma (author)
Thu Aug 21 03:55:35 -0700 2008
lifo (committer)
Thu Aug 21 07:21:21 -0700 2008
commit  ea40f71431a821b2ddb37be6ea3ee7d8dac63b85
tree    2a56eb3d76b190d19795fd7f17ca48bc29ba15a0
parent  7e4ea5f4a2fd25e06820689688e3db5a4851f8e0
...
21
22
23
24
25
 
 
26
27
28
...
21
22
23
 
 
24
25
26
27
28
0
@@ -21,8 +21,8 @@ module ActiveRecord
0
       def replace(obj, dont_save = false)
0
         load_target
0
 
0
-        unless @target.nil?
0
-          if dependent? && !dont_save && @target != obj
0
+        unless @target.nil? || @target == obj
0
+          if dependent? && !dont_save
0
             @target.destroy unless @target.new_record?
0
             @owner.clear_association_cache
0
           else
...
79
80
81
 
 
 
 
 
 
 
 
 
 
82
83
84
...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
0
@@ -79,6 +79,16 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
0
     assert_raises(ActiveRecord::RecordNotFound) { Account.find(old_account_id) }
0
   end
0
 
0
+  def test_natural_assignment_to_already_associated_record
0
+    company = companies(:first_firm)
0
+    account = accounts(:signals37)
0
+    assert_equal company.account, account
0
+    company.account = account
0
+    company.reload
0
+    account.reload
0
+    assert_equal company.account, account
0
+  end
0
+
0
   def test_assignment_without_replacement
0
     apple = Firm.create("name" => "Apple")
0
     citibank = Account.create("credit_limit" => 10)

Comments

ncr Wed Sep 03 13:01:21 -0700 2008

Congrats Jakub, waiting for more :)