diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-06-14 23:48:01 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-06-19 22:21:51 +0900 |
commit | 15e3e9cdccd8ab0d204ed9e74bf7916d6025d405 (patch) | |
tree | 2b5588e1f5936fabd7ee6b12a883b00799ce7f81 /activerecord/lib/active_record/relation.rb | |
parent | 2be5ef6a1d5baebcb749c42a48628c741570fd73 (diff) | |
download | rails-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.rb')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index c911cbe5ec..7ab9bb2d2d 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -502,17 +502,16 @@ module ActiveRecord # # => SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar' def to_sql @to_sql ||= begin - relation = self - - if eager_loading? - apply_join_dependency { |rel, _| relation = rel } - end - - conn = klass.connection - conn.unprepared_statement { - conn.to_sql(relation.arel) - } - end + if eager_loading? + apply_join_dependency do |relation, join_dependency| + relation = join_dependency.apply_column_aliases(relation) + relation.to_sql + end + else + conn = klass.connection + conn.unprepared_statement { conn.to_sql(arel) } + end + end end # Returns a hash of where conditions. @@ -622,6 +621,7 @@ module ActiveRecord if ActiveRecord::NullRelation === relation [] else + relation = join_dependency.apply_column_aliases(relation) rows = connection.select_all(relation.arel, "SQL") join_dependency.instantiate(rows, &block) end.freeze |