public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix preloading of belongs_to with null foreign key generating useless query 
[#1027 state:resolved]
fcheung (author)
Wed Dec 17 15:39:09 -0800 2008
lifo (committer)
Thu Dec 18 11:19:35 -0800 2008
commit  707d0dd3e1e8df7771073670e4257d933d2818f9
tree    97ac33fde3f7e4004d607911ad1b9a5437ddf037
parent  428e77bf0fcee4369cb8d94011141f791b8e2ba9
...
307
308
309
 
310
311
312
...
307
308
309
310
311
312
313
0
@@ -307,6 +307,7 @@ module ActiveRecord
0
 
0
         klasses_and_ids.each do |klass_and_id|
0
           klass_name, id_map = *klass_and_id
0
+          next if id_map.empty?
0
           klass = klass_name.constantize
0
 
0
           table_name = klass.quoted_table_name
...
145
146
147
148
 
149
150
151
...
705
706
707
 
708
...
145
146
147
 
148
149
150
151
...
705
706
707
708
709
0
@@ -145,7 +145,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
0
   def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once
0
     post = posts(:welcome)
0
     post.update_attributes!(:author => nil)
0
-    post = assert_queries(2) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author which is null so no query for the address
0
+    post = assert_queries(1) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author which is null so no query for the author or address
0
     assert_no_queries do
0
       assert_equal nil, post.author_with_address
0
     end
0
@@ -705,4 +705,5 @@ class EagerAssociationTest < ActiveRecord::TestCase
0
   def test_order_on_join_table_with_include_and_limit
0
     assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size
0
   end
0
+
0
 end

Comments