Ticket #6822: association_callbacks_fix.diff
| File association_callbacks_fix.diff, 4.4 kB (added by stopdropandrew, 10 months ago) |
|---|
-
test/associations_test.rb
old new 72 72 end 73 73 end 74 74 end 75 75 76 76 class AssociationProxyTest < Test::Unit::TestCase 77 77 fixtures :authors, :posts, :categorizations, :categories, :developers, :projects, :developers_projects 78 78 79 79 def test_proxy_accessors 80 80 welcome = posts(:welcome) 81 81 assert_equal welcome, welcome.author.proxy_owner 82 82 assert_equal welcome.class.reflect_on_association(:author), welcome.author.proxy_reflection 83 83 welcome.author.class # force load target 84 84 assert_equal welcome.author, welcome.author.proxy_target 85 85 86 86 david = authors(:david) 87 87 assert_equal david, david.posts.proxy_owner 88 88 assert_equal david.class.reflect_on_association(:posts), david.posts.proxy_reflection 89 89 david.posts.first # force load target 90 90 assert_equal david.posts, david.posts.proxy_target 91 91 92 92 assert_equal david, david.posts_with_extension.testing_proxy_owner 93 93 assert_equal david.class.reflect_on_association(:posts_with_extension), david.posts_with_extension.testing_proxy_reflection 94 94 david.posts_with_extension.first # force load target … … 98 98 def test_push_does_not_load_target 99 99 david = authors(:david) 100 100 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 101 109 david.categories << categories(:technology) 102 110 assert !david.categories.loaded? 103 111 assert david.categories.include?(categories(:technology)) 104 112 end 113 114 def test_push_followed_by_save_does_not_load_target 115 david = authors(:david) 105 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) 122 end 123 106 124 def test_push_does_not_lose_additions_to_new_record 107 125 josh = Author.new(:name => "Josh") 108 126 josh.posts << Post.new(:title => "New on Edge", :body => "More cool stuff!") … … 772 790 assert companies(:first_firm).save 773 791 assert_equal 3, companies(:first_firm).clients_of_firm(true).size 774 792 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 776 800 def test_build_without_loading_association 777 801 first_topic = topics(:first) 778 802 Reply.column_names … … 825 849 assert_equal 3, companies(:first_firm).clients_of_firm(true).size 826 850 end 827 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? 856 end 857 828 858 def test_find_or_initialize 829 859 the_client = companies(:first_firm).clients.find_or_initialize_by_name("Yet another client") 830 860 assert_equal companies(:first_firm).id, the_client.firm_id -
lib/active_record/associations.rb
old new 1066 1066 if association.respond_to?(:loaded?) 1067 1067 if new_record? 1068 1068 association 1069 elsif association.loaded? 1070 association.select { |record| record.new_record? } 1069 1071 else 1070 association. select { |record| record.new_record? }1072 association.target.select { |record| record.new_record? } 1071 1073 end.each do |record| 1072 1074 errors.add "#{association_name}" unless record.valid? 1073 1075 end … … 1084 1086 association 1085 1087 elsif association.respond_to?(:loaded?) && association.loaded? 1086 1088 association.select { |record| record.new_record? } 1089 elsif association.respond_to?(:loaded?) && !association.loaded? 1090 association.target.select { |record| record.new_record? } 1087 1091 else 1088 1092 [] 1089 1093 end