aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/query_methods.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-06-14 23:48:01 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-06-19 22:21:51 +0900
commit15e3e9cdccd8ab0d204ed9e74bf7916d6025d405 (patch)
tree2b5588e1f5936fabd7ee6b12a883b00799ce7f81 /activerecord/lib/active_record/relation/query_methods.rb
parent2be5ef6a1d5baebcb749c42a48628c741570fd73 (diff)
downloadrails-15e3e9cdccd8ab0d204ed9e74bf7916d6025d405.tar.gz
rails-15e3e9cdccd8ab0d204ed9e74bf7916d6025d405.tar.bz2
rails-15e3e9cdccd8ab0d204ed9e74bf7916d6025d405.zip
Ensure to calculate column aliases after all table aliases are constructed
Currently, column aliases which is used for eager loading are calculated before constructing all table aliases in FROM clause. `JoinDependency#join_constraints` constructs table aliases for `joins` first, and then always re-constructs table aliases for eager loading. If both `joins` and eager loading are given a same table association, the re-construction would cause the discrepancy between column aliases and table aliases. To avoid the discrepancy, the column aliases should be calculated after all table aliases are constructed. Fixes #30603.
Diffstat (limited to 'activerecord/lib/active_record/relation/query_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb16
1 files changed, 9 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index db9101a168..5b4ba85316 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -1018,19 +1018,19 @@ module ActiveRecord
def build_join_query(manager, buckets, join_type, aliases)
buckets.default = []
- association_joins = buckets[:association_join]
- stashed_association_joins = buckets[:stashed_join]
- join_nodes = buckets[:join_node].uniq
- string_joins = buckets[:string_join].map(&:strip).uniq
+ association_joins = buckets[:association_join]
+ stashed_joins = buckets[:stashed_join]
+ join_nodes = buckets[:join_node].uniq
+ string_joins = buckets[:string_join].map(&:strip).uniq
join_list = join_nodes + convert_join_strings_to_ast(string_joins)
alias_tracker = alias_tracker(join_list, aliases)
join_dependency = ActiveRecord::Associations::JoinDependency.new(
- klass, table, association_joins, alias_tracker
+ klass, table, association_joins
)
- joins = join_dependency.join_constraints(stashed_association_joins, join_type)
+ joins = join_dependency.join_constraints(stashed_joins, join_type, alias_tracker)
joins.each { |join| manager.from(join) }
manager.join_sources.concat(join_list)
@@ -1056,11 +1056,13 @@ module ActiveRecord
end
def arel_columns(columns)
- columns.map do |field|
+ columns.flat_map do |field|
if (Symbol === field || String === field) && (klass.has_attribute?(field) || klass.attribute_alias?(field)) && !from_clause.value
arel_attribute(field)
elsif Symbol === field
connection.quote_table_name(field.to_s)
+ elsif Proc === field
+ field.call
else
field
end