【问题标题】:how can I add depentent: :destroy when I'm using Mailboxer使用 Mailboxer 时如何添加依赖: :destroy
【发布时间】:2016-09-09 08:19:33
【问题描述】:

如何在 user.rb 中添加 depentent: :destroy 来销毁使用mailboxer创建的用户之间的消息?

我有这些表格和列。

|邮箱对话 |

|编号 |主题 | created_at |更新时间 |

|邮箱通知 |

|编号 |类型 |身体 |主题 |发件人 ID |发件人类型 |对话ID |草稿|通知代码 | notify_object_id |通知对象类型 |附件 |更新时间 | created_at |全球 |过期|发件人姓名 |

|邮箱收据 |

|编号 |接收者 ID |接收器类型 |通知ID | is_read |垃圾|已删除 |邮箱类型 | created_at |更新时间 |

我做到了

(用户.rb)

has_many :messages, :class_name => "Mailboxer::Message", :as => :sender
        has_many :receipts, -> { order(:created_at => :desc, :id => :desc) }, :class_name => "Mailboxer::Receipt", dependent: :destroy, as: :receiver

has_many :notifications, -> { where( notified_object_type: 'Assignment') },
        foreign_key: "notified_object_id", dependent: :destroy,
        class_name: "Mailboxer::Notification"

但是没有用。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    您必须在关联的 belongs_to 部分使用dependent: :destroy

    所以用户 has_many :notifications 并且在通知模型中应该说 belongs_to :user, dependent: :destroy

    因为现在它说,如果有人删除通知,该用户也将被销毁。

    【讨论】:

      猜你喜欢
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      相关资源
      最近更新 更多