Ruby on Rails: getting the max value from a DB column

后端 未结 2 900
北荒
北荒 2021-02-02 04:59

Currently I can make the straight-up SQL query on my DB:

SELECT MAX(bar) FROM table_name

And it returns

相关标签:
2条回答
  • 2021-02-02 05:27

    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.

    0 讨论(0)
  • 2021-02-02 05:32

    one more way

    Bar.select("Max(bar) as max_bar").first.max_bar
    
    0 讨论(0)
提交回复
热议问题