Report abuse


			
From d3eedfaae09942c3d99377174ddf8cd554eee0a2 Mon Sep 17 00:00:00 2001
From: rsl 
Date: Thu, 11 Sep 2008 14:07:44 -0400
Subject: [PATCH] don't hit the db if there's no ids for records

---
 .../lib/active_record/association_preload.rb       |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb
index c60850f..0c5b5aa 100644
--- a/activerecord/lib/active_record/association_preload.rb
+++ b/activerecord/lib/active_record/association_preload.rb
@@ -92,6 +92,7 @@ module ActiveRecord
       def preload_has_and_belongs_to_many_association(records, reflection, preload_options={})
         table_name = reflection.klass.quoted_table_name
         id_to_record_map, ids = construct_id_map(records)
+        return if ids.empty?
         records.each {|record| record.send(reflection.name).loaded}
         options = reflection.options

@@ -109,6 +110,7 @@ module ActiveRecord

       def preload_has_one_association(records, reflection, preload_options={})
         id_to_record_map, ids = construct_id_map(records)        
+        return if ids.empty?
         options = reflection.options
         records.each {|record| record.send("set_#{reflection.name}_target", nil)}
         if options[:through]
@@ -130,6 +132,7 @@ module ActiveRecord

       def preload_has_many_association(records, reflection, preload_options={})
         id_to_record_map, ids = construct_id_map(records)
+        return if ids.empty?
         records.each {|record| record.send(reflection.name).loaded}
         options = reflection.options

@@ -232,6 +235,8 @@ module ActiveRecord
               id
             end
           end
+          next if ids.empty?
+          
           conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} #{in_or_equals_for_ids(ids)}"
           conditions << append_conditions(options, preload_options)
           associated_records = klass.find(:all, :conditions => [conditions, ids],
-- 
1.6.0.1