From 95603695eced476125ab0dca9a12ee63539cc06b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 8 Oct 2013 13:44:48 -0700 Subject: build association tree thing on demand. `instantiate` is only called once, so there is no reason to cache the data structure on the instance and mutate itself. --- .../lib/active_record/associations/join_dependency.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index 4c4a0a95ac..c9c50da36e 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -31,7 +31,6 @@ module ActiveRecord @base_klass = base @table_joins = joins @join_parts = [JoinBase.new(base)] - @associations = {} @reflections = [] @alias_tracker = AliasTracker.new(base.connection, joins) @alias_tracker.aliased_name_for(base.table_name) # Updates the count for base.table_name to 1 @@ -70,20 +69,27 @@ module ActiveRecord parents = {} type_caster = result_set.column_type primary_key + assoc = associations records = result_set.map { |row_hash| primary_id = type_caster.type_cast row_hash[primary_key] parent = parents[primary_id] ||= join_base.instantiate(row_hash) - construct(parent, @associations, join_associations, row_hash, result_set) + construct(parent, assoc, join_associations, row_hash, result_set) parent }.uniq - remove_duplicate_results!(base_klass, records, @associations) + remove_duplicate_results!(base_klass, records, assoc) records end private + def associations + join_associations.each_with_object({}) do |assoc, tree| + cache_joined_association assoc, tree + end + end + def find_parent_part(parent) join_parts.detect do |join_part| case parent @@ -125,14 +131,14 @@ module ActiveRecord end end - def cache_joined_association(association) + def cache_joined_association(association, tree) associations = [] parent = association.parent while parent != join_base associations.unshift(parent.reflection.name) parent = parent.parent end - ref = associations.inject(@associations) do |cache,key| + ref = associations.inject(tree) do |cache,key| cache[key] end ref[association.reflection.name] ||= {} @@ -147,7 +153,6 @@ module ActiveRecord @reflections << reflection join_association = build_join_association(reflection, parent, join_type) @join_parts << join_association - cache_joined_association(join_association) end join_association when Array -- cgit v1.2.3