Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot delete or update a parent row a foreign key constraint fails rails

I have received the following error when trying to delete a value in rails "cannot delete or update a parent row a foreign key constraint fails rails"

This is perfectly acceptable to me as I don't want the user to be able to delete the value if it's associated with another value. However, what is the best way to catch the exception and then notify 'alert' the user that this value cannot be deleted?

Thank You,

TW

like image 666
Twdeveloper Avatar asked Nov 09 '22 21:11

Twdeveloper


1 Answers

There are plenty of ways to handle this gracefully. If you'd like to do a check before the destroy I recommend you take a look at this post as it shows a good example of using the before_destroy method and gives you control of the error you show. However, you can also add a rescue_from and capture the error in your controller.

like image 169
mattforni Avatar answered Nov 14 '22 22:11

mattforni