gpt4 book ai didi

ruby-on-rails - 在哪里使用依赖 : :destroy

转载 作者:行者123 更新时间:2023-12-05 09:22:03 24 4
gpt4 key购买 nike

假设 User 有很多 Receipt 并且我希望 Receipt 的所有实例在父 User 实例被删除。

写法有什么区别:

(1) 在User模型中,has_many :receipts, dependent: :destroy

(2) 在Receipt模型中,belongs_to :user, dependent: :destroy

有什么建议吗?

最佳答案

(1) In User model, has_many :receipts, dependent: :destroy

如果用户被销毁,那么其关联的收据也将被销毁。

(2) In Receipt model, belongs_to :user, dependent: :destroy

如果收据被销毁,那么它的父用户将被销毁,而不是其他相关的收据不会被销毁但会被孤立。根据docs

This dependent: option should not be specified when belongs_to is used inconjunction with a has_many relationship on another class because ofthe potential to leave orphaned records behind.

关于ruby-on-rails - 在哪里使用依赖 : :destroy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com