开发者

How to delete on cascade on Rails 2

开发者 https://www.devze.com 2023-04-11 05:34 出处:网络
I have this DB design: contracts id name allotment id name contract_id allotments开发者_运维技巧_rooms

I have this DB design:

contracts
  • id
  • name
allotment
  • id
  • name
  • contract_id
allotments开发者_运维技巧_rooms
  • 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)

0

精彩评论

暂无评论...
验证码 换一张
取 消