aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/preloader/through_association.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-09-23 18:15:38 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-09-23 18:15:38 -0700
commitbb9554ad62a3f37738ee7ad99dbbb9a37784c343 (patch)
treeda8cb33aec4a67c5e3ab459c421b21a83b44a90f /activerecord/lib/active_record/associations/preloader/through_association.rb
parent9b47142ce120d0a7d715648259ffa238526d1f82 (diff)
downloadrails-bb9554ad62a3f37738ee7ad99dbbb9a37784c343.tar.gz
rails-bb9554ad62a3f37738ee7ad99dbbb9a37784c343.tar.bz2
rails-bb9554ad62a3f37738ee7ad99dbbb9a37784c343.zip
we can't sort by lhs since the middle records have difference classes
and possibly different rules for finding those objects
Diffstat (limited to 'activerecord/lib/active_record/associations/preloader/through_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/preloader/through_association.rb38
1 files changed, 19 insertions, 19 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb
index 60f8c754f6..16a48c1db2 100644
--- a/activerecord/lib/active_record/associations/preloader/through_association.rb
+++ b/activerecord/lib/active_record/associations/preloader/through_association.rb
@@ -46,26 +46,26 @@ module ActiveRecord
}
end
- @associated_records_by_owner = through_records.each_with_object({}) { |(lhs,middles),h|
- preloader = middle_to_pl[middles.first]
-
- rhs_records = middles.flat_map { |r|
- r.send(source_reflection.name)
- }.compact
-
- if preloader && preloader.loaded?
- loaded_records = preloader.preloaded_records
- i = 0
- record_index = loaded_records.each_with_object({}) { |r,indexes|
- indexes[r] = i
- i += 1
- }
- rs = rhs_records.sort_by { |rhs| record_index[rhs] }
- else
- rs = rhs_records
+ @associated_records_by_owner = through_records.each_with_object({}) { |(lhs,center),records_by_owner|
+ pl_to_middle = center.group_by { |record| middle_to_pl[record] }
+
+ records_by_owner[lhs] = pl_to_middle.flat_map do |preloader, middles|
+ rhs_records = middles.flat_map { |r|
+ r.send(source_reflection.name)
+ }.compact
+
+ if preloader && preloader.loaded?
+ loaded_records = preloader.preloaded_records
+ i = 0
+ record_index = loaded_records.each_with_object({}) { |r,indexes|
+ indexes[r] = i
+ i += 1
+ }
+ rhs_records.sort_by { |rhs| record_index[rhs] }
+ else
+ rhs_records
+ end
end
-
- h[lhs] = rs
}
end