LoginSignup
0
0

More than 1 year has passed since last update.

【Rails】論理削除を実装しているモデルに対しては destroy を呼べないようにしたい

Posted at

モデルに👇のメソッド追加

  before_destroy :prevent_destroy
  def prevent_destroy
    raise "Destroy is not allowed for Article model. Because article model has a soft delete feature. Use discard function instead of destroy."
  end

物理削除しようとするとこうなる👇

irb(main):001:0> Article.destroy_all
/app/models/article.rb:31:in `prevent_destroy': Destroy is not allowed for Article model. Because article model has a soft delete feature. Use discard function instead of destroy. (RuntimeError)

これで安心😌

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