I have several tables with foreign key restrictions in my Rails application. For example, each order belongs to the customer. There is a costumer_id column in the order table .
When I delete a client with a placed order, due to database restrictions, MySQL returns an error:
Mysql :: Error: unable to delete or update parent line: foreign key constraint failed ( orders, CONSTRAINT orders_ibfk_2FOREIGN KEY ( customer_id) LINKS customers( id))
And an ugly error appears on the screen, with all the stacktrace and those ActiveRecord :: StatementInvalid things in DevicesController # destroy ...
I would like to know if there is an elegant way to handle these constraint errors, giving a beautiful look "you can delete this object because it is associated with X"
How can i do this?
source
share