How should I use after_create with a condition in the model
Asked Answered
D

1

17

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, such as the value of an attribute of an object

after_create :send_welcome_email unless self.role == "Celebrant"

for example?

Diaphaneity answered 6/9, 2011 at 2:56 Comment(0)
B
46

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

Bastogne answered 6/9, 2011 at 3:4 Comment(1)
the documentation details have been moved to hereExempt

© 2022 - 2024 — McMap. All rights reserved.