From 2560757ea2c41ec859a196f679cd87d9274768d2 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sun, 2 May 2021 12:56:06 +0100 Subject: [PATCH] Change Enterprise after_create callback to after_create_commit As a general rule, if you're triggering an email job as part of an after create/save callback, it should use after commit instead. Why? The transaction can't finish until after the record is persisted (the data is committed) which includes the logic in all callbacks. So for example if the transaction fails after the email job has been placed it will be rolled back, but the email job will already be in the queue, and it'll be referencing a record that doesn't actually exist (due to the rollback). --- app/models/enterprise.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index f77e634e96..b749096b94 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -105,7 +105,7 @@ class Enterprise < ApplicationRecord after_touch :touch_distributors after_create :set_default_contact after_create :relate_to_owners_enterprises - after_create :send_welcome_email + after_create_commit :send_welcome_email after_rollback :restore_permalink