Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

How to delete parent object when last associated child is deleted?

Rails • Asked by Daniel Weaver

I have a Family model that has_many Kids. I'm using dependent: :destroy on the family so when it is destroyed the associated kids are destroyed too. All good.

Now I need to destroy the family when the last associated kid is deleted. This is to prevent stray families in the database that have no kids.

Should I do it in students_controller#destroy method? Or is there a better way?

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 31,152+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.