diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-23 18:15:38 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-23 18:15:38 -0700 |
commit | bb9554ad62a3f37738ee7ad99dbbb9a37784c343 (patch) | |
tree | da8cb33aec4a67c5e3ab459c421b21a83b44a90f | |
parent | 9b47142ce120d0a7d715648259ffa238526d1f82 (diff) | |
download | rails-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
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/through_association.rb | 38 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_through_associations_test.rb | 3 |
2 files changed, 21 insertions, 20 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 diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 149c9576ca..29f6166b00 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -47,7 +47,8 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase club1 = Club.includes(:members).find_by_id club.id left, right = club1.members.map(&:id) - assert_operator left, :>, right + assert_equal [member1, member2].sort_by(&:id), + club1.members.sort_by(&:id) end def make_model(name) |