简体   繁体   English

为什么没有为paper_trail创建的“版本”触发AR回调?

[英]Why AR callbacks are not triggered for `Version`s created by paper_trail?

I'm trying to do something each time a Version is created by paper_trail by setting up a callback on the same model but it does not work. 我试图通过paper_trail每次创建一个Version时都通过在同一模型上设置回调来做一些事情,但是它不起作用。

I'm using Rails v6.0.0.beta3 and paper_trail v10.3.0. 我正在使用Rails v6.0.0.beta3和paper_trail v10.3.0。 I tried creating the Version manually ( Version.create ) and it works. 我尝试手动创建Version( Version.create ),它可以工作。

class Version
  after_commit :do_something, on: :create

  private

  def do_something
    p('doing something') # never gets called
  end
end

class SomeModel
  has_paper_trail
end

# creates both SomeModel and a Version but version's callbacks
# are not called/executed.
SomeModel.create

I expected Version#do_something to be called, but it never happens. 我希望Version#do_something被调用,但从未发生。

The issue here is that Version is not the same as PaperTrail::Version it looks like the supported mechanism for this is to create a module and pass that through See Here or creating your own class See Here – engineersmnky 这里的问题是Version与PaperTrail :: Version不同,它似乎受支持的机制是创建一个模块,并将其传递给See Here或创建自己的类See Here – Engineersmnky

@engineersmnky you were right, since I'm using a custom class I had to specify the class_name as has_paper_trail versions: { class_name: 'Version' }. @engineersmnky,您说得对,因为我使用的是自定义类,因此必须将class_name指定为has_paper_trail版本:{class_name:'Version'}。 Thanks a lot! 非常感谢! – Héctor Vásquez –赫克托·巴斯克斯(HéctorVásquez)

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

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