Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11514: inspect_on_new_object_association_does_not_reload_association_2.diff

File inspect_on_new_object_association_does_not_reload_association_2.diff, 1.9 kB (added by acirugeda, 6 months ago)
  • activerecord/test/cases/associations_test.rb

    old new  
    141141    assert_equal 1, josh.posts.size 
    142142  end 
    143143 
     144  def test_inspect_does_not_lose_additions_to_new_record 
     145    andres = Author.new(:name => "Andres") 
     146    andres.posts.build(:title => "New on Edge", :body => "More cool stuff!") 
     147    assert !andres.posts.loaded? 
     148    assert_equal 1, andres.posts.size 
     149    andres.posts.inspect 
     150    assert !andres.posts.loaded? 
     151    assert_equal 1, andres.posts.size 
     152  end 
     153 
    144154  def test_save_on_parent_does_not_load_target 
    145155    david = developers(:david) 
    146156 
  • activerecord/lib/active_record/associations/association_proxy.rb

    old new  
    7373      end 
    7474 
    7575      def inspect 
    76         reload unless loaded? 
     76        reload if needs_to_be_loaded? 
    7777        @target.inspect 
    7878      end 
    7979 
     
    137137        def load_target 
    138138          return nil unless defined?(@loaded) 
    139139 
    140           if !loaded? and (!@owner.new_record? || foreign_key_present) 
    141             @target = find_target 
    142           end 
     140          @target = find_target if needs_to_be_loaded? 
    143141 
    144142          @loaded = true 
    145143          @target 
     
    147145          reset 
    148146        end 
    149147 
     148        def needs_to_be_loaded? 
     149          !loaded? && (!@owner.new_record? || foreign_key_present) 
     150        end 
     151 
    150152        # Can be overwritten by associations that might have the foreign key available for an association without 
    151153        # having the object itself (and still being a new record). Currently, only belongs_to presents this scenario. 
    152154        def foreign_key_present