相关=>摧毁一个"通过的has_many"协会 [英] dependent => destroy on a "has_many through" association

查看:129
本文介绍了相关=>摧毁一个"通过的has_many"协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然取决于=>破坏时也使用被忽略:通过选项

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

...但删除评论不会导致相关comment_user记录越来越删除。

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

什么是推荐的方法,那么,在使用的级联删除:通过

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

感谢

推荐答案

显然:依赖也是不可忽视的。

Apparently :dependent is not ignored!

真正的问题是,我是叫 Comment.delete(ID)肚里直奔分贝,而我现在用 Comment.destroy (ID)用来加载评论对象,并调用destroy()方法就可以了。这拿起:依赖=&GT; :摧毁,一切都很好

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.

这篇关于相关=&GT;摧毁一个&QUOT;通过的has_many&QUOT;协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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