Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix variable names and speed up relation ordering

  • Loading branch information...
commit b4aae5a8e6a3d65b11490261b445faf9e38bbcd2 1 parent e5299c1
@tenderlove tenderlove authored
View
10 activerecord/lib/active_record/associations/preloader/association.rb
@@ -72,16 +72,8 @@ def options
reflection.options
end
- def preloaded_records1
- associated_records_by_owner.values.flatten
- end
-
def preloaded_records
- if owners.first.association(reflection.name).loaded?
- []
- else
- associated_records_by_owner.values.flatten
- end
+ associated_records_by_owner.values.flatten
end
def loaded?
View
18 activerecord/lib/active_record/associations/preloader/through_association.rb
@@ -27,7 +27,7 @@ def associated_records_by_owner
through_records = owners.map do |owner, h|
association = owner.association through_reflection.name
- x = [owner, Array(association.reader), association]
+ x = [owner, Array(association.reader)]
# Dont cache the association - we would only be caching a subset
association.reset if should_reset
@@ -35,7 +35,7 @@ def associated_records_by_owner
x
end
- middle_records = through_records.map { |rec| rec[1] }.flatten
+ middle_records = through_records.map { |(_,rec)| rec }.flatten
preloader = Preloader.new(middle_records,
source_reflection.name,
@@ -49,17 +49,21 @@ def associated_records_by_owner
}
end
- @associated_records_by_owner = through_records.each_with_object({}) { |(lhs,middles,assoc),h|
- x = middle_to_pl[middles.first]
+ @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 x && x.loaded?
- rs = rhs_records.sort_by { |rhs|
- x.preloaded_records1.index(rhs)
+ 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
end
Please sign in to comment.
Something went wrong with that request. Please try again.