diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index f88493df8a..24a50db619 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -1091,14 +1091,17 @@ module ActiveRecord field = klass.attribute_alias(field) if klass.attribute_alias?(field) from = from_clause.name || from_clause.value - if klass.columns_hash.key?(field) && - (!from || from == table.name || from == connection.quote_table_name(table.name)) + if klass.columns_hash.key?(field) && (!from || table_name_matches?(from)) arel_attribute(field) else yield end end + def table_name_matches?(from) + /(?:\A|(?