1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Rails】バリデーションをスキップ

Posted at

validate: false

saveにvalidate: falseを引数として与えると、saveのバリデーションをスキップすることが可能です。この手法は十分注意して使う必要があります。
save(validate: false)

  • こんな時に使用
    • データパッチをする必要があるが、バリデーションが走り登録できない
    • 仕様上、Modelに定義したバリデーションは変更したくない
sample.rb
ActiveRecord::Base.transaction do
  # 更新
  contract.assign_attributes(contract_kind: MONTHLY, updated_system: SYSTEM_NAME, updated_user: UPDATED_USER)
  contract.save!(validate: false)
end
1
1
2

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?