Sunday, November 11, 2007

Rails: Cascading Deletes

Just some deliberate Google bait. If you're wondering how to do a cascading delete in Rails, it's very easy:

class Widgets < ActiveRecord::Base
  has_many :whatevers, :dependent => :destroy

Deleting a Widget now deletes that Widget's Whatevers as well.

(Josh Susser posted almost exactly this example on a not-Google-able mailing list, I'm just paraphrasing to make it easy for newbs to find.)