依赖 =>在“has_many through"上销毁协会 [英] dependent => destroy on a "has_many through" association

查看:17
本文介绍了依赖 =>在“has_many through"上销毁协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然依赖 => 在使用 :through 选项时,destroy 将被忽略.

Apparently dependent => destroy is ignored when also using the :through option.

所以我有这个...

class Comment < ActiveRecord::Base
  has_many :comment_users, :dependent => :destroy
  has_many :users, :through => :comment_users
  ...
end

...但删除评论不会导致相关的评论用户记录被删除.

...but deleting a Comment does not result in the associated comment_user records getting deleted.

那么,对于使用 :through 时的级联删除,推荐的方法是什么?

What's the recommended approach, then, for cascade deletes when using :through?

谢谢

推荐答案

显然 :dependent 没有被忽略!

Apparently :dependent is not ignored!

真正的问题是我正在调用直接进入数据库的 Comment.delete(id),而我现在使用加载的 Comment.destroy(id)Comment 对象并对其调用 destroy() .这将获取 :dependent =>:destroy 一切顺利.

The real issue was that I was calling Comment.delete(id) which goes straight to the db, whereas I now use Comment.destroy(id) which loads the Comment object and calls destroy() on it. This picks up the :dependent => :destroy and all is well.

这篇关于依赖 =>在“has_many through"上销毁协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆