To find the maximum length of string column in SQL is:
select max(length(<column>)) from <table>
Can anyone show how to do the same in Rails 4 activerecord or even squeel?
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With