diff --git a/activerecord/lib/active_record/associations/alias_tracker.rb b/activerecord/lib/active_record/associations/alias_tracker.rb index f41f7bffffcdd0e69bcc531983ae7d17923a7e88..ea8e1f5054ae29ea1015c6f0f95c395be2ef2cba 100644 --- a/activerecord/lib/active_record/associations/alias_tracker.rb +++ b/activerecord/lib/active_record/associations/alias_tracker.rb @@ -8,8 +8,8 @@ class AliasTracker # :nodoc: attr_reader :aliases, :connection # table_joins is an array of arel joins which might conflict with the aliases we assign here - def initialize(connection, table_joins = []) - @aliases = Hash.new { |h,k| h[k] = initial_count_for(k, table_joins) } + def initialize(connection, table_joins) + @aliases = Hash.new { |h,k| h[k] = initial_count_for(k, table_joins) } @connection = connection end @@ -46,8 +46,6 @@ def aliased_name_for(table_name, aliased_name) private def initial_count_for(name, table_joins) - return 0 if Arel::Table === table_joins - # quoted_name should be downcased as some database adapters (Oracle) return quoted name in uppercase quoted_name = connection.quote_table_name(name).downcase diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index 1bc998d20ce150a267f7458d7bf0ddc784b8981a..63e81a17aade93640694ca223ce3605d33c507f1 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -12,7 +12,7 @@ def scope(association, connection) reflection = association.reflection scope = klass.unscoped owner = association.owner - alias_tracker = AliasTracker.new connection + alias_tracker = AliasTracker.new(connection, []) scope.extending! Array(reflection.options[:extend]) add_constraints(scope, owner, klass, reflection, alias_tracker) diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 01d46f767620ed4e1812ed1af6f53e1146f689db..7099bdd28583002947a602ee63df959f7a358319 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -347,7 +347,15 @@ def construct_join_dependency(joins = []) end def construct_relation_for_association_calculations - apply_join_dependency(self, construct_join_dependency(arel.froms.first)) + from = arel.froms.first + if Arel::Table === from + apply_join_dependency(self, construct_join_dependency) + else + # FIXME: as far as I can tell, `from` will always be an Arel::Table. + # There are no tests that test this branch, but presumably it's + # possible for `from` to be a list? + apply_join_dependency(self, construct_join_dependency(from)) + end end def apply_join_dependency(relation, join_dependency)