views:

114

answers:

1

model a:

has_many :b, :dependent => :delete_all

model b:

belongs_to :a
belongs_to :c

model c:

has_many :b

When I delete an a, I would also like to have children b's deleted so that they get removed from any c's that may reference them. However, the above isn't working. I'd appreciate any help.

+3  A: 

Like so:

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

Update

Your recent comment indicates you are using the delete() method to delete your objects. This will not use the callbacks. Please read the manual for specifics.

hobodave
I can't figure out why this isn't working. When I delete an 'a' that references a 'b', and that 'b' is also referenced in a 'c', the 'b' is still a child of the 'c' after deletion of 'a', using the above.
James
James, you'd have to have :dependent => :destroy for all of the objects in the chain.
Mike Buckbee
hmmm... works when I call destroy on the parent, but doesn't work when I call delete. Is this expected?
James
@James: Yes this is expected. Delete doesn't use the :dependent callbacks. See: http://www.nickpeters.net/2007/12/21/delete-vs-destroy/
hobodave
Cool, thank you.
James