モデルの条件でafth_createを使用するにはどうすればよいですか
-
26-10-2019 - |
質問
オブジェクトの作成後に呼び出される方法があります
after_create :send_welcome_email
オブジェクトの属性の値など、これを条件に制限する方法はありますか
after_create :send_welcome_email unless self.role == "Celebrant"
例えば?
解決
これを行うには、シンボル、文字列、またはProcの3つの方法があります。
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
所属していません StackOverflow