Permalink
Browse files

only calculate offset index once. #12537

  • Loading branch information...
1 parent 828134b commit 19b871a0902c4ec3e460a38f41583a7855edd81a @tenderlove tenderlove committed Oct 14, 2013
Showing with 10 additions and 6 deletions.
  1. +10 −6 activerecord/lib/active_record/associations/preloader/through_association.rb
@@ -35,6 +35,15 @@ def associated_records_by_owner(preloader)
}
end
+ pl_indexes = preloaders.each_with_object({}) do |pl,hash|
+ i = 0
+ loaded_records = pl.preloaded_records
+ hash[pl] = loaded_records.each_with_object({}) { |r,indexes|
+ indexes[r] = i
+ i += 1
+ }
+ end
+
through_records.each_with_object({}) { |(lhs,center),records_by_owner|
pl_to_middle = center.group_by { |record| middle_to_pl[record] }
@@ -43,12 +52,7 @@ def associated_records_by_owner(preloader)
r.send(source_reflection.name)
}.compact
- loaded_records = pl.preloaded_records
- i = 0
- record_index = loaded_records.each_with_object({}) { |r,indexes|
- indexes[r] = i
- i += 1
- }
+ record_index = pl_indexes[pl]
records = rhs_records.sort_by { |rhs| record_index[rhs] }
@preloaded_records.concat rhs_records
records

0 comments on commit 19b871a

Please sign in to comment.