Quantcast
Channel: User BvuRVKyUVlViVIc7 - Stack Overflow
Viewing all articles
Browse latest Browse all 42

Answer by BvuRVKyUVlViVIc7 for How to find the longest string in a column with Ruby on Rails?

$
0
0

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)

Viewing all articles
Browse latest Browse all 42

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>