LoginSignup
1
0

More than 1 year has passed since last update.

rails バリデーション

Last updated at Posted at 2021-07-31

バリデーション

バリデーションとは・・不正なデータがデータベースに保存されないようにデータベースをチェックする仕組み。バリデーションに引っかかった場合(不正なデータの場合)にはデータベースには保存されない。

バリデーションの例

Postモデルのバリデーション

models/post.rb
class Post<ApplicationRecord
validates :content,{presence:true}
       #検証するカラム名   検証する内容
end

=>contentカラムが存在しているか

models/user.rb
class User<ApplicationRecord
validates :email,{uniqueness:true}
       #検証するカラム名   検証する内容
end

=>emailで新たにユーザー登録できないようにするためにemailの重複がないか

1
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
1
0