Permalink
Browse files

combine methods so we can reuse preloaders

  • Loading branch information...
1 parent a901433 commit 99568a7a2d17f236dee0d41aa69a637f698b5275 @tenderlove tenderlove committed Sep 21, 2013
Showing with 18 additions and 19 deletions.
  1. +18 −19 activerecord/lib/active_record/associations/preloader/through_association.rb
@@ -12,7 +12,24 @@ def source_reflection
end
def associated_records_by_owner
- through_records = through_records_by_owner
+ left_loader = Preloader.new(owners,
+ through_reflection.name,
+ through_scope)
+ left_loader.run
+
+ should_reset = (through_scope != through_reflection.klass.unscoped) ||
+ (reflection.options[:source_type] && through_reflection.collection?)
+
+ through_records = owners.map do |owner, h|
+ association = owner.association through_reflection.name
+
+ x = [owner, Array(association.reader), association]
+
+ # Dont cache the association - we would only be caching a subset
+ association.reset if should_reset
+
+ x
+ end
middle_records = through_records.map { |rec| rec[1] }.flatten
@@ -30,24 +47,6 @@ def associated_records_by_owner
private
- def through_records_by_owner
- Preloader.new(owners, through_reflection.name, through_scope).run
-
- should_reset = (through_scope != through_reflection.klass.unscoped) ||
- (reflection.options[:source_type] && through_reflection.collection?)
-
- owners.map do |owner, h|
- association = owner.association through_reflection.name
-
- x = [owner, Array(association.reader), association]
-
- # Dont cache the association - we would only be caching a subset
- association.reset if should_reset
-
- x
- end
- end
-
def through_scope
scope = through_reflection.klass.unscoped

0 comments on commit 99568a7

Please sign in to comment.