With MySql:
# Change your model name and field to your needsParticipant.order("MAX(CHAR_LENGTH(first_name)) desc").limit(1)# works too:Participant.order("MAX(CHAR_LENGTH(first_name)) desc").first# and this is the most efficient to get the field directly:Participant.limit(1).order("MAX(CHAR_LENGTH(first_name)) desc").pluck(:first_name)
With postgres:
Participants.limit(1).order("MAX(CHAR_LENGTH(name)) desc").group("id").pluck(:name)