[英]Rails 4 how to find the maximum length of a string column
To find the maximum length of string column in SQL is:在 SQL 中查找字符串列的最大长度是:
select max(length(<column>)) from <table>
Can anyone show how to do the same in Rails 4 activerecord or even squeel?任何人都可以展示如何在 Rails 4 activerecord 甚至 squeel 中做同样的事情吗?
您可以使用Model.pluck("max(length(column))")
,它不会将所有内容加载到内存中。
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:另一种选择是只在 SQL 中执行查询:
Model.connection.execute("SELECT MAX(LENGTH(column)) FROM my_table")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.