Triggering Notifications Based on Enum Changes
You can use after_commit
callbacks to send emails, notifications, or webhooks when an enum changes.
Example: Sending an Email When an Order is Shipped
class Order < ApplicationRecord
enum :status, { pending: 0, confirmed: 1, shipped: 2, delivered: 3 }
after_commit :send_shipping_notification, if: :saved_change_to_status?
private
def send_shipping_notification
OrderMailer.shipped_email(self).deliver_later if status == "shipped"
end
end
Now, every time an order's status is updated to "shipped", an email is automatically sent.