aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-09-23 16:10:12 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-09-23 16:10:12 -0700
commitb4aae5a8e6a3d65b11490261b445faf9e38bbcd2 (patch)
tree3726231ad86b8be21f5a42acd18f9f8d57ae206e /activerecord
parente5299c1ef693ef434f55811027a7da975cd55ba5 (diff)
downloadrails-b4aae5a8e6a3d65b11490261b445faf9e38bbcd2.tar.gz
rails-b4aae5a8e6a3d65b11490261b445faf9e38bbcd2.tar.bz2
rails-b4aae5a8e6a3d65b11490261b445faf9e38bbcd2.zip
fix variable names and speed up relation ordering
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/preloader/association.rb10
-rw-r--r--activerecord/lib/active_record/associations/preloader/through_association.rb18
2 files changed, 12 insertions, 16 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb
index 6c60d03998..a57284fd06 100644
--- a/activerecord/lib/active_record/associations/preloader/association.rb
+++ b/activerecord/lib/active_record/associations/preloader/association.rb
@@ -72,16 +72,8 @@ module ActiveRecord
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?
diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb
index a62815830c..09d1281ccc 100644
--- a/activerecord/lib/active_record/associations/preloader/through_association.rb
+++ b/activerecord/lib/active_record/associations/preloader/through_association.rb
@@ -27,7 +27,7 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
}
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