In order for ‘dependent’ to work and all child records to be destroyed when parent is, does the child foreign key have to be the id of the parent record ?
parent table | id |
child table | id |parent_id|
or could the foreign key be another field that ties all the records together, say a user_id ?
This is correct. In the relationship definition you can use the option
:dependent => :destroy, this will delete all the records in the child
table that refers back to its parent. If there are no other
dependencies of the child records then you can use :dependent =>
:delete_all option. This will issue the SQL query in one shot and
improve the efficiency.