Say I have:
class X < ActiveRecord::Base
has_many :z, :dependent => :destroy
end
class Y < ActiveRecord::Base
has_many :z, :dependent => :destroy
end
class Z < ActiveRecord::Base
belongs_to :x
belongs_to :y
end
… and then I:
x.destroy
Does z get destroyed if it also belongs to y?
I know it’s pretty simple to test and I’ll do so and answer my own question if I don’t get any responses, but in addition to the correct answer I’m also interested in better knowing how :dependent => :destroy works and if there are any other implications I need to be aware of. The ActiveRecord::Base documentation is pretty sparse on this topic.
It will for sure destroy all z objects connected, and that will make a lot of business errors on your database.
IMHO I would refactor this modeling. Work with this kind of relationship could go terribly bad. Is there a reason you can’t remodel?
Maybe if just change the validation could work, or even make X and Y inherit some ohter class that makes the relationhip with the X class.