public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix two has_one :through errors

* Set the association target on assignment;
* Reset target to nil on reset, rather than empty array.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#895 state:committed]
pivotal (author)
Tue Aug 26 09:20:24 -0700 2008
NZKoz (committer)
Wed Aug 27 02:24:45 -0700 2008
commit  e710902f26a2eed7adb22082067df449b9641d00
tree    10741d1229503cc9a0a4cb8878584c539f73d0b3
parent  b4d13a97cd90cd453119f7deb264fc6c1b190c10
...
1111
1112
1113
1114
 
 
1115
1116
1117
1118
1119
1120
...
1111
1112
1113
 
1114
1115
1116
 
 
1117
1118
1119
0
@@ -1111,10 +1111,9 @@ module ActiveRecord
0
               association.create_through_record(new_value)
0
               self.send(reflection.name, new_value)
0
             else
0
-              association.replace(new_value)              
0
+              association.replace(new_value)
0
+              instance_variable_set(ivar, new_value.nil? ? nil : association)
0
             end
0
-
0
-            instance_variable_set(ivar, new_value.nil? ? nil : association)
0
           end
0
 
0
           define_method("set_#{reflection.name}_target") do |target|
...
22
23
24
 
 
 
 
25
26
27
...
22
23
24
25
26
27
28
29
30
31
0
@@ -22,6 +22,10 @@ module ActiveRecord
0
     
0
       def find_target
0
         super.first
0
+      end
0
+
0
+      def reset_target!
0
+        @target = nil
0
       end        
0
     end        
0
   end
...
101
102
103
 
 
 
 
 
 
 
 
 
104
...
101
102
103
104
105
106
107
108
109
110
111
112
113
0
@@ -101,4 +101,13 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
0
     assert_equal clubs(:crazy_club), members[0].sponsor_club
0
   end
0
 
0
+  def test_uninitialized_has_one_through_should_return_nil_for_unsaved_record
0
+    assert_nil Member.new.club
0
+  end
0
+
0
+  def test_assigning_association_correctly_assigns_target
0
+    new_member = Member.create(:name => "Chris")
0
+    new_member.club = new_club = Club.create(:name => "LRUG")
0
+    assert_equal new_club, new_member.club.target
0
+  end
0
 end

Comments