是否在HABTM关系中的联接表中调用了ActiveRecord回调? [英] Are ActiveRecord callbacks called in the join table in an HABTM relationship?

查看:132
本文介绍了是否在HABTM关系中的联接表中调用了ActiveRecord回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试 acts_as_audited 时,我发现(也如

While testing acts_as_audited, I discovered (as also described here) that the :with_associations flag does not produce audit table entries for HABTM relationships.

例如:

User < ActiveRecord::Base
  has_and_belongs_to_many: :groups
  acts_as_audited, with_associations: groups

Group < ActiveRecord::Base
  has_and_belongs_to_many: :users
  acts_as_audited, with_associations: users

(以及经过测试的变体,即带有/不带有with_associations)

(and tested variations, ie. with/without with_associations)

在源代码中,可以看到act_as_audited所做的所有事情就是在已审计的表中添加了诸如before_update和after_create之类的回调.显然这些没有添加到联接表中.

In the source, one can see that all acts_as_audited does is adds callbacks like before_update and after_create to the audited tables. Apparently these are not added to the join tables.

我试图制作一个像这样的模型:

I tried making a model like:

GroupsUsers < ActiveRecord::Base
  acts_as_audited

  after_save: :test

  def test
    logger.debug "test"
  end

,但未在用户或组的CRUD操作的审核表中看到任何其他内容.我可以在日志中看到对联接表起作用的SQL语句,因此这表明联接表在内部已被更改,从而可以绕过正常的回调.

but did not see any additions to the audit table for CRUD operations on Users or Groups. I can see the SQL statement acting on the join table in the logs so this suggests that the join table is altered internally in such a way that the normal callbacks are bypassed.

这是真的吗?有什么建议可以使act_as_audited注意到连接表或记录HABTM关联?

Is this true? Any suggestions for getting acts_as_audited to notice the join table or to log HABTM associations?

推荐答案

关联回调has_and_belongs_to_many

类似于连接到Active Record对象生命周期的普通回调,您还可以定义在向关联集合中添加对象或从关联集合中删除对象时触发的回调.

Association callbacks has_and_belongs_to_many

Similar to the normal callbacks that hook into the life cycle of an Active Record object, you can also define callbacks that get triggered when you add an object to or remove an object from an association collection.

class Project
  has_and_belongs_to_many :developers, after_add: :evaluate_velocity

  def evaluate_velocity(developer)
    ...
  end
end

可以通过将回调作为数组传递来堆叠回调.示例:

It's possible to stack callbacks by passing them as an array. Example:

class Project
  has_and_belongs_to_many :developers,
                          after_add: [:evaluate_velocity, Proc.new { |p, d| p.shipping_date = Time.now}]
end

可能的回调是:before_addafter_addbefore_removeafter_remove.

Possible callbacks are: before_add, after_add, before_remove and after_remove.

如果任何before_add回调引发异常,则该对象将不会添加到集合中.

If any of the before_add callbacks throw an exception, the object will not be added to the collection.

类似地,如果任何before_remove回调引发异常,则不会从集合中删除该对象.

Similarly, if any of the before_remove callbacks throw an exception, the object will not be removed from the collection.

这篇关于是否在HABTM关系中的联接表中调用了ActiveRecord回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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