Through many iterations of testing, I just noticed that my join table that represents a HABTM relationship between two models isn’t removing entries when instances of these models get deleted. Do I need to do something special when removing an instance of a model that has HABTM relationships?
Through many iterations of testing, I just noticed that my join table that represents
Share
Upon closer inspection HABTM relationships should be removing join table entries. However neither HABTM relationships or the relationship I described in the original version (see post history) of this solution will remove those join table entries when you eliminate the record with the
deletemethod.ActiveRecord::Base#deletedoes not trigger any callbacks, such as the ones a HABTM relationship establishes to remove orphaned entries from the join table. Instead you should be usingActiveRecord::Base#destroy.You will have to use raw SQL to remove the unneeded entries. If you decide to create a join model, you can iterate through entries in the join model, deleting those without an association.
Example: