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|
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.