Post

with_optionsでスッキリ

after

with_options if: :new_record? do before_validation :initialize_hoge1 before_validation :initialize_hoge2 end def initialize_hoge1 hoge1 end def initialize_hoge2 hoge2 end

before

before_validation :initialize_hoge1 before_validation :initialize_hoge2 def initialize_hoge1 hoge1 if new_record? end def initialize_hoge2 hoge2 if new_record? end

Railsで特定の条件下で走るバリデーションを作りたい - Qiita

This post is licensed under CC BY 4.0 by the author.