如何在 Rails 2 上级联删除
数据库设计:
contracts- id
- name
- id
- namecontract_id
- 我有这样的
- id
- allotment_id
- room_id
- 1contract 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
所以,当我删除合同时,Allotment被完美删除,但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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用
:dependent =>; :delete_all
,您销毁依赖记录没有调用他们的destroy方法,这样他们就没有机会销毁自己的关联记录。尝试:dependent => :delete
代替。如果你想在数据库级别设置 FK 约束,我还建议使用 foreigner gem (尽管我不这样做)不知道它是否适用于 Rails 2)
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)