Is there a way to trigger a callback on a Rails model only after a commit has happened for a create? -
i calling "queue" method after_create
callback trigger sidekiq process on model instance after created. however, first time sidekiq worker picks job, record hasn't been committed yet.
i know there after_commit
record, want object queued when created, not when updated. how can accomplish without hackery involving date-checking, etc.?
you set flag in before_create
callback
before_create :set_new_flag after_commit :queue def set_new_flag @__new_flag = true end def queue if defined? @__new_flag #queue remove_instance_variable(:@__new_flag) end # puts self.previous_changes end
or inspect #previous_changes
in after_commit
. nil
in id key should new_records. here's example output of previous_changes
{"name"=>[nil, "newest"], "id"=>[nil, "cbdjzqgxir45xqj9uxgrsr"], "version_id"=>[nil, "cbdnaogxir45xqj9uxgrsr"]}
Comments
Post a Comment