Skip to content
This repository
Browse code

Calling ActiveRecord#inspect on an unloaded association won't wipe th…

…e collection [#9 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
commit bcb090c56b842a76397e0ea32f54c942fd11910e 1 parent 2561732
Andreas Neuhaus authored May 08, 2008 josh committed May 08, 2008
4  activerecord/lib/active_record/associations/association_proxy.rb
@@ -118,7 +118,7 @@ def target=(target)
118 118
       end
119 119
 
120 120
       def inspect
121  
-        reload unless loaded?
  121
+        load_target
122 122
         @target.inspect
123 123
       end
124 124
 
@@ -167,7 +167,7 @@ def merge_options_from_reflection!(options)
167 167
         def with_scope(*args, &block)
168 168
           @reflection.klass.send :with_scope, *args, &block
169 169
         end
170  
-          
  170
+
171 171
       private
172 172
         def method_missing(method, *args)
173 173
           if load_target
6  activerecord/test/cases/associations_test.rb
@@ -149,6 +149,12 @@ def test_save_on_parent_does_not_load_target
149 149
     assert !david.projects.loaded?
150 150
   end
151 151
 
  152
+  def test_inspect_does_not_reload_a_not_yet_loaded_target
  153
+    andreas = Developer.new :name => 'Andreas', :log => 'new developer added'
  154
+    assert !andreas.audit_logs.loaded?
  155
+    assert_match(/message: "new developer added"/, andreas.audit_logs.inspect)
  156
+  end
  157
+
152 158
   def test_save_on_parent_saves_children
153 159
     developer = Developer.create :name => "Bryan", :salary => 50_000
154 160
     assert_equal 1, developer.reload.audit_logs.size
4  activerecord/test/models/developer.rb
@@ -49,6 +49,10 @@ def find_least_recent
49 49
   before_create do |developer|
50 50
     developer.audit_logs.build :message => "Computer created"
51 51
   end
  52
+
  53
+  def log=(message)
  54
+    audit_logs.build :message => message
  55
+  end
52 56
 end
53 57
 
54 58
 class AuditLog < ActiveRecord::Base

0 notes on commit bcb090c

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