I’ve got two models Category and Item. Category can have many items and item can be in many categories so relation between them is HABTM.
My problem is that a category can be deleted even if items are in it.
I have a foreign key in the database table categories_items that RESTRICTS deletion, but it doesn’t help.
What should I do to prevent category from being deleted if items are in it?
I’ve got two models Category and Item. Category can have many items and item
Share
You need to overwrite the actual “delete” method in your CategoriesController to verify that there are no Items in the category before it is deleted.
Something like…