Rails 4 how to find the maximum length of a string column
You can use Model.pluck("max(length(column))")
, which won't load everything into memory.
Something like this?
Model.pluck(:column).max_by(&:length)
#=> will return the longest string
Another option would be to just execute a query in SQL:
Model.connection.execute("SELECT MAX(LENGTH(column)) FROM my_table")