简体   繁体   English

通过 has_many 删除/销毁 join_table 上的记录

[英]Delete/Destroy record on join_table with has_many through

I render a set of checkboxes and I want to delete all the Privilege of a User .我渲染了一组复选框,我想删除User的所有Privilege

  • When I check one of the checkboxes, a record is created.当我选中其中一个复选框时,会创建一条记录。
  • When I uncheck one of the checkboxes, a record is deleted.当我取消选中其中一个复选框时,一条记录被删除。

I have searched all the questions related, but unluckily none works.我已经搜索了所有相关的问题,但不幸的是没有一个有效。

Rails 5.2.3导轨 5.2.3

User用户

has_many :user_privileges, class_name: 'UserPrivileges'
has_many :privileges, through: :user_privileges

Privilege特权

has_many :user_privileges, class_name: 'UserPrivileges'
has_many :users, through: :user_privileges

UserPrivileges用户权限

belongs_to :user
belongs_to :privilege

The issue kick in when I want to delete ( uncheck ) the last privilege-record of that user in the join_table .当我想删除(取消选中)该用户在join_table中的最后一个权限记录时,问题就出现了。

The record is still there, and there is no way to delete/destroy that specific record.该记录仍然存在,并且无法删除/销毁该特定记录。

My intuition recall to the callbacks, I have tried different ways of using dependent but the last record is still there.我的直觉回想起回调,我尝试了不同的使用dependent的方式,但最后一条记录仍然存在。

Any tips are welcome.欢迎任何提示。

Thanks谢谢

If you want to delete the record from the join table, you need to add dependent: :destroy to has_many:through relationship.如果要从连接表中删除记录,则需要在has_many:through关系中添加dependent: :destroy

# privilege.rb
has_many :user_privileges, class_name: 'UserPrivileges'
has_many :users, through: :user_privileges, dependent: :destroy

See What gets deleted?请参阅删除什么? in API docs :API 文档中:

There is a potential pitfall here: has_and_belongs_to_many and has_many:through associations have records in join tables, as well as the associated records.这里有一个潜在的陷阱:has_and_belongs_to_many 和 has_many:through 关联在连接表中都有记录,以及关联的记录。 So when we call one of these deletion methods, what exactly should be deleted?那么当我们调用其中一种删除方法时,究竟应该删除什么?

The answer is that it is assumed that deletion on an association is about removing the link between the owner and the associated object(s), rather than necessarily the associated objects themselves.答案是假设删除关联是关于删除所有者和关联对象之间的链接,而不是关联对象本身。 So with has_and_belongs_to_many and has_many:through, the join records will be deleted, but the associated records won't.因此,使用 has_and_belongs_to_many 和 has_many:through,连接记录将被删除,但关联记录不会。

To run dependent: :destroy callback, you must use the destroy or destroy_all method when deleting the privilege record.要运行dependent: :destroy回调,您必须在删除权限记录时使用destroydestroy_all方法。

See Delete or destroy?请参阅删除或销毁? in API docs :API 文档中:

For has_many, destroy and destroy_all will always call the destroy method of the record(s) being removed so that callbacks are run.对于 has_many,destroy 和 destroy_all 将始终调用要删除的记录的 destroy 方法,以便运行回调。 However delete and delete_all will either do the deletion according to the strategy specified by the:dependent option, or if no:dependent option is given, then it will follow the default strategy.但是,delete 和 delete_all 将根据:dependent 选项指定的策略进行删除,或者如果给出 no:dependent 选项,则它将遵循默认策略。 The default strategy is to do nothing (leave the foreign keys with the parent ids set), except for has_many:through, where the default strategy is delete_all (delete the join records, without running their callbacks).默认策略是什么都不做(保留设置了父 ID 的外键),除了 has_many:through,默认策略是 delete_all(删除连接记录,不运行它们的回调)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM