I have 3 models A, B and C. B is the join table between A & C. The association is made through a has_many :through
.
I was wondering if the non-join-table models (A & C in my case) should have dependent: :destroy
with the join table association or if it's taken care automatically by rails ?
Is it the same answer for a HABTM association ?
No, because you can delete records without instantiating them which wouldn't call dependent destroy and you'd be left with orphaned records.
For example delete_all
Instead if you add a foreign key the database will handle the delete and it doesn't matter if you instantiate the object or not.
For example in a migration you can add
def change
add_foreign_key :as, :bs, on_delete: :cascade
end
Or in the table creation migration
t.belongs_to :a, foreign_key: { on_delete: :cascade }