How should I use after_create with a condition in the model

前端 未结 1 1504
春和景丽
春和景丽 2021-02-07 07:48

I have a method that is called after the creation of an object

after_create :send_welcome_email

Is there a way to limit this to a condition, su

1条回答
  •  北荒
    北荒 (楼主)
    2021-02-07 08:39

    There are three ways to do this: Symbol, String, or Proc.

    class User < ActiveRecord::Base
    
      after_create :send_welcome_email, unless: :is_celebrant?
      after_create :send_welcome_email, unless: "is_celebrant?"
      after_create :send_welcome_email, unless: Proc.new { self.role == "Celebrant" }
    
    end
    

    Documentation

    0 讨论(0)
提交回复
热议问题