#Link
[Run different ActiveRecord validations based on context | Boring Rails: Skip the bullshit and ship fast](https://boringrails.com/tips/activerecord-validation-context)
[[🗃️Rails]]の[[🗃️ActiveRecord]]で、[[🗃️バリデーション]]をskipするときに、validate: falseを使うのではなく、onのオプションでcontextを渡すとよいよという話。
```ruby
class Listing < ApplicationRecord
belongs_to :company
belongs_to :user
validates :title, presence: true, length: { maximum: 50 }
# context: :publishで保存したときだけvalidationが発火する
validates :salary_range, presence: true, on: :publish
validates :application_instructions, presence: true, on: :publish
def publish!
self.published_at = Time.current
save(context: :publish)
end
end
```
onは[[🗃️バリデーション]]の実行タイミングを制御できる
[Active Record バリデーション - Railsガイド](https://railsguides.jp/active_record_validations.html#on)
だいぶ前のバージョンからはいっている処理だけど、知らなかったな