LoginSignup
0
0

More than 3 years have passed since last update.

バリデーションの設定で同じ記述をまとめる

Posted at

バリデーションの設定で空白ではDBに登録できなくするpresence: trueはよく用いられると思います。
今まではカラム一つに対してpresence: trueを毎回記述していましたがwith_optionsを使うことで無駄な記述を省略してコードが読みやすくなりました。
例えば

   validates :title, presence: true
   validates :name, presence: true
   validates :text, presence: true

このpresence: trueをwith_optionsを使うことで

   with_options presence: true do
    validates :title
    validates :name
    validates :text
   end

スッキリとしたコードに編集することができました。

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