After I create a record, I send an email, which I do in an after_commit
callback. I want to save the Message-Id
header of the email as an attribute on the record to use later. I implemented this as:
after_commit on: :create do
if email = Mailer.email(self).deliver
# `self.message_id = email.message_id` has no effect, so I'm calling update()
self.update message_id: email.message_id
end
end
Surprisingly (to me), this causes an infinite email-sending loop (sorry, Mailgun); it appears that the callback is called on update even though on: :create
is specified.
Is there something wrong with this approach that I'm not seeing? How else could I attach this value to this record?
My only thought is to try gating the callback on previous_changes
, but in any case I'd like to understand why this doesn't work as-is.