Destroy order in model


I have a model Person that has a Bag that has Content.
Person has Content as well.

A Bag cannot be destroyed if it is not empty.

In Person:
has_many :bags, :dependent => :destroy # A
has_many :contents, :dependent => :destroy # B

In Bag:
has_many :contents, :dependent => :destroy # C

If I destroy a Person with a non empty Bag, I get an error (correct).
By swapping A and B the error disappears.
(First destroy all Content => all Bags are empy)

This is what I wanted, but I could not find this documented, so I don’t
know if I can rely on this

Do you know anything about this?


Does every “content” belong to a bag? If so, you’re probably trying to
delete the same thing twice.

You might try getting rid of the dependent statement on #b, and let the
bag destroy its contents…


The question is:
“can I rely on the destroy order to be the same of order the declaration

In other words: can I assume, given

class Person < ActiveRecord::Base
has_many :bags, :dependent => :destroy # A
has_many :contents, :dependent => :destroy # B

that destroying an object of type Person
first destroys bags and only then contents?

This is already happening, but I am not sure that it is not by accident


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs