Destroy on has_many relationships


#1

i need to delete a record from the database and delete all of it’s
children as well. is there a method for this? right now, i am finding
all the children with the parent_id and destroying them first and then
destroying the parent. seems like there would be a better way to do this
but i can’t seem to figure it out.

thanks,
josh


#2

i need to delete a record from the database and delete all of it’s
children as well. is there a method for this? right now, i am finding
all the children with the parent_id and destroying them first and then
destroying the parent. seems like there would be a better way to do this
but i can’t seem to figure it out.

ok, i think i figured it out. in the model i put something like

has_many :whatevers, :dependent => :destroy

now when i delete the parent model, it destroy’s all the children as
well. if there is a better way, please let me know.


#3

Josh K. wrote:

i need to delete a record from the database and delete all of it’s
children as well. is there a method for this? right now, i am finding
all the children with the parent_id and destroying them first and then
destroying the parent. seems like there would be a better way to do this
but i can’t seem to figure it out.

ok, i think i figured it out. in the model i put something like

has_many :whatevers, :dependent => :destroy

now when i delete the parent model, it destroy’s all the children as
well. if there is a better way, please let me know.

That is the preferred method.