0
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.

ActiveRecord::InvalidForeignKey in RoomsController#destroy エラーの解決方法

Posted at

エラー内容

・ActiveRecord::InvalidForeignKey in RoomsController#destroy というエラーが表示された。
ActiveRecord InvalidForeignKey in RoomsController destroy.png

原因

・dependent漏れでした。

・ちなみにこんな記事もありました。 dependent: :delete_all を使っている方はこちらが原因かも?
https://stackoverflow.com/questions/45522580/rails-activerecordinvalidforeignkey-in-articlescontrollerdestroy

Using dependent: :delete_all does not use validation thus it is directly deleting records without validating >properly. Use dependent: :destroy if you want your records to be validated safely.

↓日本語訳↓
dependent: :delete_all はバリデーションを行わないので、適切にバリデーションを行わずに直接レコードを削除することになります。安全に検証(バリデーション)を行いたい場合は dependent: :destroy を使用します。

解決方法

・以下のようにdependent: :destroyを設定すればOK

image21.png

0
1
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
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?