Permalink
Browse files

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.
  • Loading branch information...
1 parent ebe4f80 commit 95603695eced476125ab0dca9a12ee63539cc06b @tenderlove tenderlove committed Oct 8, 2013
Showing with 11 additions and 6 deletions.
  1. +11 −6 activerecord/lib/active_record/associations/join_dependency.rb
View
17 activerecord/lib/active_record/associations/join_dependency.rb
@@ -31,7 +31,6 @@ def initialize(base, associations, joins)
@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 @@ def instantiate(result_set)
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 @@ def remove_duplicate_results!(base, records, associations)
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 @@ def build(associations, parent, join_type)
@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

0 comments on commit 9560369

Please sign in to comment.