Topic: Removing Children from SQL

Me again - getting bored of me yet?

Is there a better way of doing this? The code deletes all the of the task list's children then deletes itself:

    list = TaskList.find_by_id(params[:id])
    list.tasks.each do |task|
    redirect_to(:action    => 'index')

Re: Removing Children from SQL

Yep, check out the has_many docs. Particularly the :dependent parameter. If you set that to destroy it will automatically delete all tasks when you destroy a task list:

class TaskList < ActiveRecord::Base
  has_many :tasks, :dependent => :destroy

Edit: Oh, btw, I was answering the 2nd question, not the first. I'm not bored of you. smile

Last edited by ryanb (2006-09-04 11:11:21)

Railscasts - Free Ruby on Rails Screencasts

Re: Removing Children from SQL

That wasn't in my book wink Thanks