From 9dba824b12dc60656b324b1197b9ea38d2d53d11 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Sun, 14 Dec 2008 23:03:31 +0000 Subject: [PATCH] Fix preloading of belongs_to with null foreign query generating useless query --- .../lib/active_record/association_preload.rb | 1 + activerecord/test/cases/associations/eager_test.rb | 3 ++- 2 files changed, 3 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 99c3ce5..d8aa105 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -307,6 +307,7 @@ module ActiveRecord klasses_and_ids.each do |klass_and_id| klass_name, id_map = *klass_and_id + next if id_map.empty? klass = klass_name.constantize table_name = klass.quoted_table_name diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 3c8408d..19d1f55 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -145,7 +145,7 @@ class EagerAssociationTest < ActiveRecord::TestCase def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once post = posts(:welcome) post.update_attributes!(:author => nil) - 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 + 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 assert_no_queries do assert_equal nil, post.author_with_address end @@ -705,4 +705,5 @@ class EagerAssociationTest < ActiveRecord::TestCase def test_order_on_join_table_with_include_and_limit assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size end + end -- 1.6.0.1