Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Railsで「整数のみ」、「0以上の正の数のみ」のvalidatesの書き方

環境

Rails 5.1.7
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]

結論

 validates :price, presence: true, numericality: {only_integer: true, greater_than_or_equal_to: 0}

解説

「整数のみ」の箇所
only_integer: true,

「0以上の正の数のみ」の箇所
greater_than_or_equal_to: 0

syohhe
現在25歳。WEB制作に興味を持ち、勉強中。 エラーの解決や日々の気付きを発信中。 ◾️スキル HTML / CSS / JavaScript / Ruby / Ruby on rails
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away