aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/lib/active_record/associations/join_dependency.rb26
-rw-r--r--activerecord/lib/active_record/associations/join_dependency/join_association.rb6
-rw-r--r--activerecord/lib/active_record/associations/join_dependency/join_base.rb4
-rw-r--r--activerecord/lib/active_record/associations/join_dependency/join_part.rb23
4 files changed, 22 insertions, 37 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb
index 3141e6e850..7dffc3e3e5 100644
--- a/activerecord/lib/active_record/associations/join_dependency.rb
+++ b/activerecord/lib/active_record/associations/join_dependency.rb
@@ -90,12 +90,22 @@ module ActiveRecord
i[column.name] = column.alias
}
}
+ @name_and_alias_cache = tables.each_with_object({}) { |table,h|
+ h[table.name] = table.columns.map { |column|
+ [column.name, column.alias]
+ }
+ }
end
def columns
@tables.flat_map { |t| t.column_aliases }
end
+ # An array of [column_name, alias] pairs for the table
+ def column_aliases(table)
+ @name_and_alias_cache[table]
+ end
+
def column_alias(table, column)
@alias_cache[table][column]
end
@@ -128,18 +138,17 @@ module ActiveRecord
seen = Hash.new { |h,parent_klass|
h[parent_klass] = Hash.new { |i,parent_id|
- i[parent_id] = Hash.new { |j,child_klass|
- j[child_klass] = {}
- }
+ i[parent_id] = Hash.new { |j,child_klass| j[child_klass] = {} }
}
}
model_cache = Hash.new { |h,klass| h[klass] = {} }
parents = model_cache[join_root]
+ column_aliases = aliases.column_aliases join_root.table
result_set.each { |row_hash|
primary_id = type_caster.type_cast row_hash[primary_key]
- parent = parents[primary_id] ||= join_root.instantiate(row_hash)
+ parent = parents[primary_id] ||= join_root.instantiate(row_hash, column_aliases)
construct(parent, join_root, row_hash, result_set, seen, model_cache, aliases)
}
@@ -206,7 +215,7 @@ module ActiveRecord
raise EagerLoadPolymorphicError.new(reflection)
end
- node = JoinAssociation.new(reflection, join_root.to_a.length, join_type)
+ node = JoinAssociation.new(reflection, join_type)
construct_tables!(parent, node)
node
end
@@ -235,15 +244,16 @@ module ActiveRecord
if model
construct(model, node, row, rs, seen, model_cache, aliases)
else
- model = construct_model(ar_parent, node, row, model_cache, id)
+ model = construct_model(ar_parent, node, row, model_cache, id, aliases)
seen[parent.base_klass][primary_id][node.base_klass][id] = model
construct(model, node, row, rs, seen, model_cache, aliases)
end
end
end
- def construct_model(record, node, row, model_cache, id)
- model = model_cache[node][id] ||= node.instantiate(row)
+ def construct_model(record, node, row, model_cache, id, aliases)
+ model = model_cache[node][id] ||= node.instantiate(row,
+ aliases.column_aliases(node.table))
other = record.association(node.reflection.name)
if node.reflection.collection?
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb
index d83c01c819..a6a7b447cc 100644
--- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb
+++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb
@@ -10,17 +10,13 @@ module ActiveRecord
# What type of join will be generated, either Arel::InnerJoin (default) or Arel::OuterJoin
attr_accessor :join_type
- # These implement abstract methods from the superclass
- attr_reader :aliased_prefix
-
attr_accessor :tables
- def initialize(reflection, index, join_type)
+ def initialize(reflection, join_type)
super(reflection.klass)
@reflection = reflection
@join_type = join_type
- @aliased_prefix = "t#{ index }"
@tables = nil
end
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_base.rb b/activerecord/lib/active_record/associations/join_dependency/join_base.rb
index de03f7097d..d6280796d5 100644
--- a/activerecord/lib/active_record/associations/join_dependency/join_base.rb
+++ b/activerecord/lib/active_record/associations/join_dependency/join_base.rb
@@ -9,10 +9,6 @@ module ActiveRecord
super && base_klass == other.base_klass
end
- def aliased_prefix
- "t0"
- end
-
def table
Arel::Table.new(table_name, arel_engine)
end
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_part.rb b/activerecord/lib/active_record/associations/join_dependency/join_part.rb
index 476288b7c5..ab78231bd7 100644
--- a/activerecord/lib/active_record/associations/join_dependency/join_part.rb
+++ b/activerecord/lib/active_record/associations/join_dependency/join_part.rb
@@ -41,29 +41,12 @@ module ActiveRecord
raise NotImplementedError
end
- # The prefix to be used when aliasing columns in the active_record's table
- def aliased_prefix
- raise NotImplementedError
- end
-
# The alias for the active_record's table
def aliased_table_name
raise NotImplementedError
end
- # An array of [column_name, alias] pairs for the table
- def column_names_with_alias
- unless @column_names_with_alias
- @column_names_with_alias = []
-
- column_names.each_with_index do |column_name, i|
- @column_names_with_alias << [column_name, "#{aliased_prefix}_r#{i}"]
- end
- end
- @column_names_with_alias
- end
-
- def extract_record(row)
+ def extract_record(row, column_names_with_alias)
# This code is performance critical as it is called per row.
# see: https://github.com/rails/rails/pull/12185
hash = {}
@@ -80,8 +63,8 @@ module ActiveRecord
hash
end
- def instantiate(row)
- base_klass.instantiate(extract_record(row))
+ def instantiate(row, aliases)
+ base_klass.instantiate(extract_record(row, aliases))
end
end
end