Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

we can't sort by lhs since the middle records have difference classes

and possibly different rules for finding those objects
  • Loading branch information...
commit bb9554ad62a3f37738ee7ad99dbbb9a37784c343 1 parent 9b47142
@tenderlove tenderlove authored
View
38 activerecord/lib/active_record/associations/preloader/through_association.rb
@@ -46,26 +46,26 @@ def associated_records_by_owner(preloader)
}
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
View
3  activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -47,7 +47,8 @@ def test_preload_sti_middle_relation
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)
Please sign in to comment.
Something went wrong with that request. Please try again.