Tbpgr Blog

元エンジニア 人事 tbpgr(てぃーびー) のブログ

Ruby on Rails | Validation | numericality

概要

numericality

詳細

ActiveRecordで、validation時にnumericalityを利用することで、
数値のみを保持しているかどうかチェックを行うことができます。

オプション一覧

option 内容
only_integer 整数のみ
greater_than より大きい
greater_than_or_equal_to 以上
equal_to 同じ
less_than 未満
less_than_or_equal_to 以下
odd 奇数
even 偶数

サンプル

テーブル定義
CREATE TABLE "articles" (
  "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
  "title" varchar(255), 
  "text" text, 
  "created_at" datetime, 
  "updated_at" datetime
);
Validationその1
class Article < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  validates :title, numericality: true
  validates :text,  numericality: { only_integer: true }
end
試行その1

rails console(pry)で試行します。

[1] pry(main)> a = Article.new
=> #<Article id: nil, title: nil, text: nil, created_at: nil, updated_at: nil>
[2] pry(main)> a.title = 'text'
=> "text"
[3] pry(main)> a.text = 1.2
=> 1.2
[4] pry(main)> a.valid?
=> false
[5] pry(main)> a.errors.messages
=> {:title=>["is not a number"], :text=>["must be an integer"]}
[6] pry(main)> a.text = 2
=> 2
[7] pry(main)> a.title = 1.2
=> 1.2
[8] pry(main)> a.valid?
=> true
[9] pry(main)> a.errors.messages
=> {}
Validationその2
class Article < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  validates :title, numericality: { greater_than: 10  }
  validates :text,  numericality: { greater_than_or_equal_to: 10 }
end
試行その2

rails console(pry)で試行します。

[1] pry(main)> a = Article.new
=> #<Article id: nil, title: nil, text: nil, created_at: nil, updated_at: nil>
[2] pry(main)> a.title = 10
=> 10
[3] pry(main)> a.text = 9
=> 9
[4] pry(main)> a.valid?
=> false
[5] pry(main)> a.errors.messages
=> {:title=>["must be greater than 10"],
 :text=>["must be greater than or equal to 10"]}
[6] pry(main)> a.title = 11
=> 11
[7] pry(main)> a.text = 10
=> 10
[8] pry(main)> a.valid?
=> true
[9] pry(main)> a.errors.messages
=> {}
Validationその3
class Article < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  validates :title, numericality: { less_than: 10  }
  validates :text,  numericality: { less_than_or_equal_to: 10 }
end
試行その3

rails console(pry)で試行します。

[1] pry(main)> a = Article.new
=> #<Article id: nil, title: nil, text: nil, created_at: nil, updated_at: nil>
[2] pry(main)> a.title = 10
=> 10
[3] pry(main)> a.text = 11
=> 11
[4] pry(main)> a.valid?
=> false
[5] pry(main)> a.errors.messages
=> {:title=>["must be less than 10"], :text=>["must be less than or equal to 10"]}
[6] pry(main)> a.title = 9
=> 9
[7] pry(main)> a.text = 10
=> 10
[8] pry(main)> a.valid?
=> true
[9] pry(main)> a.errors.messages
=> {}
Validationその4
class Article < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  validates :title, numericality: { equal_to: 10  }
end
試行その4

rails console(pry)で試行します。

[1] pry(main)> a = Article.new
=> #<Article id: nil, title: nil, text: nil, created_at: nil, updated_at: nil>
[2] pry(main)> a.title = 9
=> 9
[3] pry(main)> a.valid?
=> false
[4] pry(main)> a.errors.messages
=> {:title=>["must be equal to 10"]}
[5] pry(main)> a.title = 10
=> 10
[6] pry(main)> a.valid?
=> true
[7] pry(main)> a.errors.messages
=> {}
Validationその5
class Article < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  validates :title, numericality: {odd: true}
  validates :text,  numericality: {even: true}
end
試行その5

rails console(pry)で試行します。

[1] pry(main)> a = Article.new
=> #<Article id: nil, title: nil, text: nil, created_at: nil, updated_at: nil>
[2] pry(main)> a.title = 2
=> 2
[3] pry(main)> a.text = 1
=> 1
[4] pry(main)> a.valid?
=> false
[5] pry(main)> a.errors.messages
=> {:title=>["must be odd"], :text=>["must be even"]}
[6] pry(main)> a.title = 1
=> 1
[7] pry(main)> a.text = 2
=> 2
[8] pry(main)> a.valid?
=> true
[9] pry(main)> a.errors.messages
=> {}