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

后端 未结 2 906
北荒
北荒 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.

提交回复
热议问题