I found that after_commit on: :create callbacks on activerecord models are not working when using them with shrine.
I create a simple model to replicate the issue
class Catalog < ApplicationRecord after_create :after_create_callback after_update :after_update_callback after_commit :after_commit_create_callback, on: :create after_commit :after_commit_update_callback, on: :update def after_create_callback Rails.logger.debug "After Create Triggered" end def after_update_callback Rails.logger.debug "After Update Triggered" end def after_commit_create_callback Rails.logger.debug "After Commit-Create Triggered" end def after_commit_update_callback Rails.logger.debug "After Commit-Update Triggered" end include FileUploader::Attachment.new(:file) end class FileUploader < Shrine plugin :instrumentation plugin :activerecord, callbacks: true end
When creating a new model like this
catalog = Catalog.new catalog.name = "New" catalog.file = File.new("test.jpg","rb") catalog.save
The line After Commit-CreateTriggered is never printed, but After Commit-Update Triggered is printed instead.
To notice that the after_create callback do work (but i can’t use it on my use case), is only the after_commit on:create that is not working.
If i set callbacks as false in the activerecord plugin configuration on the attacher
plugin :activerecord, callbacks: false
The after_commit on:create lines are correctly printed, so the issue is only there when having the activerecord plugin callbacks enabled.
So after tracing the code, when having the callbacks:true on the plugin the problem arise on the persist call in activerecord_after_save method of the activerecord plugin, that if i’m not wrong just saves the record without validation.
def activerecord_after_save finalize persist end
I know is the persist line because if i remove the persist line and only keep the finalize one, the after_commit on: :create works again.
Do you think there is an alternative to get this working, with the alternative not being removing the after_commit callback on activerecord models?
Thanks in advance!