diff options
-rw-r--r-- | activerecord/lib/active_record/associations/join_dependency.rb | 21 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 4 |
2 files changed, 20 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index 4635601781..94255fc74f 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -82,13 +82,26 @@ module ActiveRecord make_joins join_root end - def columns - join_root.collect { |join_part| + class Aliases + def initialize(tables) + @tables = tables + end + + def columns + @tables.flat_map { |t| t.columns } + end + + Table = Struct.new(:table, :columns) + end + + def aliases + Aliases.new join_root.map { |join_part| table = join_part.aliased_table - join_part.column_names_with_alias.collect{ |column_name, aliased_name| + columns = join_part.column_names_with_alias.collect{ |column_name, aliased_name| table[column_name].as Arel.sql(aliased_name) } - }.flatten + Aliases::Table.new(table, columns) + } end def instantiate(result_set) diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 92969cfb30..5e11ba0325 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -243,7 +243,9 @@ module ActiveRecord def find_with_associations join_dependency = construct_join_dependency relation = except :select - relation = relation.select(join_dependency.columns) + + aliases = join_dependency.aliases + relation = relation.select aliases.columns relation = apply_join_dependency(relation, join_dependency) if block_given? |