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

Changeset 4721

Show
Ignore:
Timestamp:
08/08/06 16:59:06 (2 years ago)
Author:
rick
Message:

Cache nil results for has_one associations so multiple calls don't call the database. Closes #5757. [Michael A. Schoen]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r4706 r4721  
    11*SVN* 
     2 
     3* Cache nil results for has_one associations so multiple calls don't call the database.  Closes #5757. [Michael A. Schoen] 
    24 
    35* Add documentation for how to disable timestamps on a per model basis. Closes #5684. [matt@mattmargolis.net Marcel Molina Jr.]  
  • trunk/activerecord/lib/active_record/associations.rb

    r4690 r4721  
    657657            before_save <<-EOF 
    658658              association = instance_variable_get("@#{reflection.name}") 
    659               if !association.nil?  
     659              if association && association.target 
    660660                if association.new_record? 
    661661                  association.save(true) 
     
    842842              association = association_proxy_class.new(self, reflection) 
    843843              retval = association.reload 
    844               unless retval.nil? 
    845                 instance_variable_set("@#{reflection.name}", association) 
    846               else 
     844              if retval.nil? and association_proxy_class == BelongsToAssociation 
    847845                instance_variable_set("@#{reflection.name}", nil) 
    848846                return nil 
    849847              end 
    850             end 
    851             association 
     848              instance_variable_set("@#{reflection.name}", association) 
     849            end 
     850 
     851            association.target.nil? ? nil : association 
    852852          end 
    853853 
  • trunk/activerecord/lib/active_record/associations/association_proxy.rb

    r4398 r4721  
    127127          if !@owner.new_record? || foreign_key_present 
    128128            begin 
    129               @target = find_target if !loaded? 
     129              @target = find_target unless loaded? 
    130130            rescue ActiveRecord::RecordNotFound 
    131131              reset 
     
    133133          end 
    134134 
    135           loaded if target 
     135          loaded 
    136136          target 
    137137        end 
  • trunk/activerecord/test/associations_test.rb

    r4690 r4721  
    9595    assert_equal companies(:first_firm).account, Account.find(1) 
    9696    assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit 
     97  end 
     98 
     99  def test_has_one_cache_nils 
     100    assert_nil companies(:another_firm).account 
     101    assert_queries(0) { companies(:another_firm).account } 
    97102  end 
    98103