0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails】整数のバリデーション

Posted at

0以上の整数のみを許可

class Product < ApplicationRecord
  validates :stock, numericality: { greater_than_or_equal_to: 0, only_integer: true }
end
  • greater_than_or_equal_to: 0 で0以上を指定
  • only_integer: true で整数のみを許可

1以上の整数のみを許可

class Order < ApplicationRecord
  validates :quantity, numericality: { greater_than: 0, only_integer: true }
end

まとめ

条件 バリデーション
0以上の整数 numericality: { greater_than_or_equal_to: 0, only_integer: true }
1以上の整数 numericality: { greater_than: 0, only_integer: true }
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?