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

Changeset 8504

Show
Ignore:
Timestamp:
12/28/07 18:01:22 (6 months ago)
Author:
rick
Message:

Don't unnecessarily load has_many associations in after_update callbacks. Closes #6822 [stopdropandrew, canadaduane]

Files:

Legend:

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

    r8456 r8504  
    11*SVN* 
     2 
     3* Don't unnecessarily load has_many associations in after_update callbacks.  Closes #6822 [stopdropandrew, canadaduane] 
    24 
    35* Eager belongs_to :include infers the foreign key from the association name rather than the class name.  #10517 [Jonathan Viney] 
  • trunk/activerecord/lib/active_record/associations.rb

    r8481 r8504  
    10801080              if new_record? 
    10811081                association 
     1082              elsif association.loaded? 
     1083                association.select { |record| record.new_record? } 
    10821084              else 
    1083                 association.select { |record| record.new_record? } 
     1085                association.target.select { |record| record.new_record? } 
    10841086              end.each do |record| 
    10851087                errors.add "#{association_name}" unless record.valid? 
     
    10981100            elsif association.respond_to?(:loaded?) && association.loaded? 
    10991101              association.select { |record| record.new_record? } 
     1102            elsif association.respond_to?(:loaded?) && !association.loaded? 
     1103              association.target.select { |record| record.new_record? } 
    11001104            else 
    11011105              [] 
  • trunk/activerecord/test/associations_test.rb

    r8486 r8504  
    7373  end 
    7474end 
    75  
     75  
    7676class AssociationProxyTest < Test::Unit::TestCase 
    7777  fixtures :authors, :posts, :categorizations, :categories, :developers, :projects, :developers_projects 
    78  
     78   
    7979  def test_proxy_accessors 
    8080    welcome = posts(:welcome) 
     
    8383    welcome.author.class  # force load target 
    8484    assert_equal  welcome.author, welcome.author.proxy_target 
    85  
     85   
    8686    david = authors(:david) 
    8787    assert_equal  david, david.posts.proxy_owner 
     
    8989    david.posts.first   # force load target 
    9090    assert_equal  david.posts, david.posts.proxy_target 
    91  
     91   
    9292    assert_equal  david, david.posts_with_extension.testing_proxy_owner 
    9393    assert_equal  david.class.reflect_on_association(:posts_with_extension), david.posts_with_extension.testing_proxy_reflection 
     
    9999    david = authors(:david) 
    100100 
     101    david.posts << (post = Post.new(:title => "New on Edge", :body => "More cool stuff!")) 
     102    assert !david.posts.loaded? 
     103    assert david.posts.include?(post) 
     104  end 
     105 
     106  def test_push_has_many_through_does_not_load_target 
     107    david = authors(:david) 
     108 
    101109    david.categories << categories(:technology) 
    102110    assert !david.categories.loaded? 
    103111    assert david.categories.include?(categories(:technology)) 
     112  end 
     113   
     114  def test_push_followed_by_save_does_not_load_target 
     115    david = authors(:david) 
     116 
     117    david.posts << (post = Post.new(:title => "New on Edge", :body => "More cool stuff!")) 
     118    assert !david.posts.loaded? 
     119    david.save 
     120    assert !david.posts.loaded? 
     121    assert david.posts.include?(post) 
    104122  end 
    105123 
     
    773791    assert_equal 3, companies(:first_firm).clients_of_firm(true).size 
    774792  end 
    775  
     793   
     794  def test_build_followed_by_save_does_not_load_target 
     795    new_client = companies(:first_firm).clients_of_firm.build("name" => "Another Client") 
     796    assert companies(:first_firm).save 
     797    assert !companies(:first_firm).clients_of_firm.loaded? 
     798  end 
     799   
    776800  def test_build_without_loading_association 
    777801    first_topic = topics(:first) 
     
    824848    companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}]) 
    825849    assert_equal 3, companies(:first_firm).clients_of_firm(true).size 
     850  end 
     851 
     852  def test_create_followed_by_save_does_not_load_target 
     853    new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client") 
     854    assert companies(:first_firm).save 
     855    assert !companies(:first_firm).clients_of_firm.loaded? 
    826856  end 
    827857