Skip to content

Instantly share code, notes, and snippets.

@AquaGeek
Created May 14, 2011 02:35
Show Gist options
  • Select an option

  • Save AquaGeek/971844 to your computer and use it in GitHub Desktop.

Select an option

Save AquaGeek/971844 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #6726
From e5fafbf5220e7346ce718ffed1c6f1135269f7e8 Mon Sep 17 00:00:00 2001
From: Kir Maximov <[email protected]>
Date: Tue, 19 Apr 2011 23:00:18 +0200
Subject: [PATCH] Speedup associations preloading for case of many records + many :include options
---
.../lib/active_record/association_preload.rb | 10 +++++++---
1 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb
index 7acc415..0e12f7a 100644
--- a/activerecord/lib/active_record/association_preload.rb
+++ b/activerecord/lib/active_record/association_preload.rb
@@ -86,17 +86,21 @@ module ActiveRecord
def preload_associations(records, associations, preload_options={})
records = [records].flatten.compact.uniq
return if records.empty?
+ preload_associations_for_normalized_records(records, associations, preload_options)
+ end
+
+ def preload_associations_for_normalized_records(records, associations, preload_options={})
case associations
- when Array then associations.each {|association| preload_associations(records, association, preload_options)}
+ when Array then associations.each {|association| preload_associations_for_normalized_records(records, association, preload_options)}
when Symbol, String then preload_one_association(records, associations.to_sym, preload_options)
when Hash then
associations.each do |parent, child|
raise "parent must be an association name" unless parent.is_a?(String) || parent.is_a?(Symbol)
- preload_associations(records, parent, preload_options)
+ preload_associations_for_normalized_records(records, parent, preload_options)
reflection = reflections[parent]
parents = records.map {|record| record.send(reflection.name)}.flatten.compact
unless parents.empty?
- parents.first.class.preload_associations(parents, child)
+ parents.first.class.preload_associations_for_normalized_records(parents, child)
end
end
end
--
1.6.1.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment