質問

オブジェクトの作成後に呼び出される方法があります

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

ドキュメンテーション

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top