dependent => destroy on a "has_many through" association

Apparently :dependent is not ignored!

The real issue was that I was calling Comment.delete(id) which goes straight to the db, whereas I now use Comment.destroy(id) which loads the Comment object and calls destroy() on it. This picks up the :dependent => :destroy and all is well.


The original poster's solution is valid, however I wanted to point out that this only works if you have an id column for that table. I prefer my many-to-many tables to only be the two foreign keys, but I had to remove my "id: false" from the migration table definition for cascading delete to work. Having this functionality definitely outweighs not having an id column on the table.


If you have a polymorphic association, you should do what @blogofsongs said but with a foreign_key attribute like so:

class User < ActiveRecord::Base
  has_many :activities , dependent: :destroy, foreign_key: :trackable_id
end