def column_alias_for(field)
        return field if field.match?(/\A\w{,#{connection.table_alias_length}}\z/)

        column_alias = +field
        column_alias.gsub!(/\*/, "all")
        column_alias.gsub!(/\W+/, " ")
        column_alias.strip!
        column_alias.gsub!(/ +/, "_")

        connection.table_alias_for(column_alias)
      end
def column_alias_for(field)
        return field if field.match?(/\A\w{,#{connection.table_alias_length}}\z/)

        column_alias = +field
        column_alias.gsub!(/\*/, "all")
        column_alias.gsub!(/\W+/, " ")
        column_alias.strip!
        column_alias.gsub!(/ +/, "_")

        connection.table_alias_for(column_alias)
      end
Converts the given field to the value that the database adapter returns as
 a usable column name:

   column_alias_for("users.id")                 # => "users_id"
   column_alias_for("sum(id)")                  # => "sum_id"
   column_alias_for("count(distinct users.id)") # => "count_distinct_users_id"
   column_alias_for("count(*)")                 # => "count_all"