Rails after_commit / after_save hooks on same instance?

669 Views Asked by At

I am planning to use some transient attributes of a model which I set on initialization of an object, and then reading it in an after_save or after_commit callback. The only issue I can foresee is, in a multiple server environment, if after_save is called on some other instance, I don't have access to the transient variable. So, wanted to know whether there is a possibility that after_save callbacks are executed on a different instance than where it was created / updated?

Example of what I am trying to do:

u = User.new email: 'x'
u.transient_attr = 'y'

Definition of User class:

class User{
  after_save :check_something

  def check_something
    if transient_attr == 'y'
      //do something
    end
  end

}

As you see, I am initiating the object and setting the transient attribute. Is there a guarantee that when I receive the after_save callback, the transient attribute that I had set on my object, will be available?

0

There are 0 best solutions below