diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-10-14 13:04:57 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-10-14 13:04:57 -0700 |
commit | 19b871a0902c4ec3e460a38f41583a7855edd81a (patch) | |
tree | f7f5c6ec80c086fd852281c32098ef4a58d6b380 | |
parent | 828134b7561bf4473580d76bd8d7ae97e9b1db92 (diff) | |
download | rails-19b871a0902c4ec3e460a38f41583a7855edd81a.tar.gz rails-19b871a0902c4ec3e460a38f41583a7855edd81a.tar.bz2 rails-19b871a0902c4ec3e460a38f41583a7855edd81a.zip |
only calculate offset index once. #12537
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/through_association.rb | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb index 3166df57eb..63d4253e6a 100644 --- a/activerecord/lib/active_record/associations/preloader/through_association.rb +++ b/activerecord/lib/active_record/associations/preloader/through_association.rb @@ -35,6 +35,15 @@ module ActiveRecord } 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 @@ module ActiveRecord 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 |