views:

32

answers:

1

I currently have this kind of setup:

:procedures, has_many => :steps

Steps are tied to the procedures that they were created under. Currently my method destroy for Procedures is just this:

def destroy
    @procedure.destroy
end

Is it necessary to go find all the steps associated with this procedure and invoke their destroy methods, or will Rails handle this for me automatically?

+3  A: 

You can get the dependent sub-items included in the destroy by using the ':dependent => :destroy' option.

So in your case it'd be:

has_many => :steps, :dependent => :destroy

Which will destroy steps when the procedure is destroyed.

You could also use:

has_many => :steps, :dependent => :delete_all

Which from the rails site

The :destroy and :delete_all option symbols are so named because they correspond with the behavior achieved by calling destroy versus delete on a model object. One triggers callbacks, the other just generates the delete SQL statement

So the delete_all won't trigger destroy callbacks and destroy will.

paulthenerd
How would I add the :dependent option if I used block notation? I added in my code sample to the question.
Karl
Nevermind, this goes in the model not the route, what am I thinking..
Karl