diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-20 15:43:46 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-20 15:43:46 -0700 |
commit | 31744e41f2c483dbb5d281cd9e170338c0ff55c9 (patch) | |
tree | c290c34e811452421ee0d81461ae1a8d36fa052e /activerecord/lib | |
parent | c6fbbbc17440b6629fc41fef1a6fc80a1830f982 (diff) | |
download | rails-31744e41f2c483dbb5d281cd9e170338c0ff55c9.tar.gz rails-31744e41f2c483dbb5d281cd9e170338c0ff55c9.tar.bz2 rails-31744e41f2c483dbb5d281cd9e170338c0ff55c9.zip |
push running the preloaders up
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/preloader.rb | 26 |
1 files changed, 7 insertions, 19 deletions
diff --git a/activerecord/lib/active_record/associations/preloader.rb b/activerecord/lib/active_record/associations/preloader.rb index 03c66cab4f..274a3352fc 100644 --- a/activerecord/lib/active_record/associations/preloader.rb +++ b/activerecord/lib/active_record/associations/preloader.rb @@ -98,18 +98,20 @@ module ActiveRecord def run_preload(associations, records) unless records.empty? - associations.each { |association| preload(association, records) } + associations.flat_map { |association| + preload(association, records) + }.each(&:run) end end def preload(association, records) case association when Hash - preload_hash(association, records).each(&:run) + preloaders_for_hash(association, records) when Symbol - preloaders_for_one(association, records).each(&:run) + preloaders_for_one(association, records) when String - preloaders_for_one(association.to_sym, records).each(&:run) + preloaders_for_one(association.to_sym, records) else raise ArgumentError, "#{association.inspect} was not recognised for preload" end @@ -121,15 +123,7 @@ module ActiveRecord loaders = preloaders_for_one parent, records recs = loaders.flat_map(&:target_records).uniq - lls = Array.wrap(child).flat_map { |assoc| - case assoc - when Hash then preloaders_for_hash(assoc, recs) - when Symbol then preloaders_for_one(assoc, recs) - when String then preloaders_for_one(assoc.to_sym, recs) - else - raise - end - } + lls = Array.wrap(child).flat_map { |assoc| preload assoc, recs } loaders + lls end @@ -144,12 +138,6 @@ module ActiveRecord # Additionally, polymorphic belongs_to associations can have multiple associated # classes, depending on the polymorphic_type field. So we group by the classes as # well. - def preload_one(association, records) - preloaders_for_one(association, records).each { |loader| - loader.run - } - end - def preloaders_for_one(association, records) grouped_records(association, records).flat_map do |reflection, klasses| klasses.map do |rhs_klass, rs| |