To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.
MAX can be used with numeric, character, and datetime columns, but not with bit columns.
The SQL MIN() and MAX() Functions The MAX() function returns the largest value of the selected column.
The SQL MAX function is used to return the maximum value of an expression in a SELECT statement.
Assuming your model name is Bar
and it has a column named bar
, this should work:
Bar.maximum("bar")
See the excellent Rails Guides section on Calculations for more info.
one more way
Bar.select("Max(bar) as max_bar").first.max_bar
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