HMT collection_singular_ids =缺失加盟模式是直接的,没有破坏回调触发 [英] HMT collection_singular_ids= deletion of join models is direct, no destroy callbacks are triggered

查看:109
本文介绍了HMT collection_singular_ids =缺失加盟模式是直接的,没有破坏回调触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

恰好碰到了一个问题,一个的has_many:通过关联和后/前灭回调未触发

Just ran into an issue with a has_many :through association and after/before-destroy callbacks not being triggered.

我说,我有用户,组,并呼吁会员中间的关系。 我有一个表单,允许用户进行注册成组通过创建一个新的会员记录时,他们检查了相关的复选框。 group_ids的基本数组。

Say, I have users, groups, and an intermediate relation called membership. I have a form that allows users to be enrolled into groups by creating a new membership record when they check off associated checkboxes. Basically an array of group_ids.

看起来是这样的:

Which group would you like to join? (check all that apply)
[] Group A
[] Group B
[] Group C

和我想记录,如加入组或离开组,活动日志表的行动,做一些其他不太重要的thigns。

And I wish to record actions such as joining a group or leaving a group to activity log table and do some other less important thigns.

我有以下定义:

class Group < AR::Base
  has_many :memberships
  has_many :users, :through => :memberships
end

class Membership < AR::Base
  belongs_to :user
  belongs_to :group

  after_create :log_event_to_audit_table
  after_destroy :log_event_to_audit_table

end

class User < ActiveRecord::Base
  has_many :memberships
  has_many :groups, :through => :memberships

  attr_accessible :group_ids   # enables mass-assignment
end

在一个新的会员记录创建after_create正在按预期运行。但是,after_destroy不会被触发!

When a new membership record is created the after_create is run as expected. However, the after_destroy does not get triggered!

在谷歌,荷兰国际集团,并宣读了文档,我发现了原因:

After google-ing and read up the docs I discovered the reason why:

自动删除加盟模式是   直接的,没有破坏回调   引发 - 从红宝石指南

"Automatic deletion of join models is direct, no destroy callbacks are triggered" - from Ruby Guides.

Hmmmmmm ...

Hmmmmmm...

因此​​,连接模型的(在这种情况下,会员的)破坏回调不被触发。嗯,这是一个令人沮丧。任何理由,为什么?

So the join model's (in this case Membership's) destroy callbacks are not being triggered. Well that's a downer. Any reason as to why?

所以我的问题是什么来解决这个问题的最好方法是什么? 我应该定义自己的membership_ids =在用户模式的方法是直接调用membership.destroy?

So my question what is the best way to work around this issue? Should I define my own membership_ids= method in User model that calls membership.destroy directly?

打开至约在这种情况下的最佳实践的任何建议。

Open to any suggestions about the best practices in such a scenario.

谢谢!

推荐答案

在仔细检查API文档,原来的has_many和HABTM有几个选项只是这种情况:

After carefully examining API docs, turns out has_many and habtm have a few options just for this case:

before_add,after_add,before_remove和after_remove

before_add, after_add, before_remove and after_remove

class User < ActiveRecord::Base
  has_many :groups, :through => :memberships, :after_remove => :your_custom_method
end

由多少回应我来看,这一定不会是一个很好的记录/使用功能。

Judging by how many responses I got, this must not be a very well documented/used feature.

只是指出在这里为自己和其他人谁可能会绊倒像我一样。

Just noting it here for myself and others who may stumble like I did.

这篇关于HMT collection_singular_ids =缺失加盟模式是直接的,没有破坏回调触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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