I have this DB design:
contracts- id
- name
- id
- name
- contract_id
- id
- allotment_id
- room_id
- 1 contract has 1 allotment
- 1 allotment has N allotments_rooms
So…
Contract has_one :allotment, :dependent => :delete_all
Allotment belongs_to :contract
Allotment has_many :allotments_rooms, :dependent => :delete_all
Allotments_Rooms belongs_to :allotment
So, when I delete a Contract, the Allotment is perfectly removed, but the allotments_rooms are not.
Why?
Thank you!
when you use :dependent => :delete_all
, you destroy dependent records without calling their destroy method, so they have no chance to destroy their own associated records. Try :dependent => :delete
instead.
i would also recommend using the foreigner gem if you want to set FK constraints at DB level (though i don't know if it works with rails 2)
精彩评论