Remove columns from the SELECT statement without specifying all the others


Since upgrading to MySQL 8, we’ve found that having TEXT columns in our SELECT statement leads to MySQL running out of sort memory, so we’re going through removing the ones we don’t need. At the moment, the only way we can do this is with a select or reselect which includes all the other columns, which is tiresome and leaves our code fragile in the face of future migrations adding new columns.

So, we’ve added a method to ActiveRecord::Relation to let us remove fields from the SELECT - here is a simplified version:

def deselect(*fields)
  if select_values.any?
    select(select_values - fields)
    select(column_names - fields)

This would need some shaping up to work with different sorts of input e.g. fields defined using a hash, but I think it would be nice if this was available in Rails out of the box. What do you all think - is this addition worth pursuing?

1 Like