简体   繁体   中英

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.

I'm using Rails v6.0.0.beta3 and paper_trail v10.3.0. I tried creating the Version manually ( Version.create ) and it works.

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.

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

@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' }. Thanks a lot! – Héctor Vásquez

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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