LoginSignup
21
18

More than 5 years have passed since last update.

【Rails4】カラムの最大値を求める(maximum)

Last updated at Posted at 2014-05-27

最大値を取得するにはmaximumを使用する

例:

max = Comment.where("image_id = ?", comment.image_id).maximum(:comment_no)
if max.nil?
  comment.comment_no = 1
else
  comment.comment_no = max + 1
end

参考にしたサンプルの書式

User.maximum(:login_count)
User.where("age > ?", 20).maximum(:login_count)

参考URL:
http://railsdoc.com/references/maximum

21
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
18