LoginSignup
19
19

More than 5 years have passed since last update.

ActiveRecord Validationの書き方メモ

Last updated at Posted at 2012-12-26

カラムを軸とした場合のvalidationの書き方の例

validates :column,
  :presence => true,  # 必須
  :uniqueness => true, # ユニーク制約
  :uniqueness => {:scope => :column2}, # 複数カラムでのユニーク制約
  :numericality => only_integer, # 整数のみ
  :length => { :in => 10..100 },  # 長さ制約(〜以上、〜以下)
  :length => { :maximum => 100 } # 長さ制約(〜以下)
  :format => { :with => /regex/ } #正規表現マッチ

独自定義のメソッドで検証

validate :validate_example

def validate_example
  errors_add
end
19
19
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
19
19