"after_create" for STI rails code example

Example 1: after_create for STI rails

after_create : create_coupons_for_existing_users, unless: :discounted_coupon?

Example 2: after_create for STI rails

module Concerns::CouponDistribution
  extend ActiveSupport::Concern

  included do
    after_create :create_coupons_for_existing_users, if: Proc.new {|cd| cd.type == "DiscountedCoupon" }
  end

  def create_coupons_for_existing_users
    #
  end

end

Example 3: after_create for STI rails

def discounted_coupon?
  self.type == "DiscountedCoupon"
end

Example 4: after_create for STI rails

def create_coupons_for_existing_users
  return unless self.type == "DiscountedCoupon"
  # your logic here
end

Tags:

Ruby Example