🖥

Rails - presence と inclusion / format などのバリデーションで両方でモデルのバリデーションエラーが発生す

に公開

解決

  • presence を指定する
  • 他のバリデーションでは allow_nil: true を設定する

と成り立つようだ

ちなみに allow_nil は「値がnilの場合に、そのバリデーションをスキップする」という動き

  validates :some_attribute, presence: true
  validates :some_attribute, inclusion: { in: [1,2,3] }, allow_nil: true

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

プロフィール・経歴

https://github.com/YumaInaura/YumaInaura

公開日時

2024-12-03

https://qiita.com/YumaInaura/items/9dd86b5337b1e7ce0702

Discussion