0

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?

shiladitya
  • 2,290
  • 1
  • 23
  • 36
  • Can you provide some sample code of what you are planning to do? Anyway: If you manipulate a record on multiple instance simultaneously, of course there is a risk of race conditions. – Wukerplank Apr 10 '17 at 09:54
  • added some sample of the idea – shiladitya Apr 10 '17 at 09:59
  • @Wukerplank I am not trying to update the same record in multiple machines, just wanted to know if callbacks are received on the same machine or not, so as to guarantee my transient attributes are available during callbacks. – shiladitya Apr 10 '17 at 10:00
  • can you paste what have you tried so far? – Jagdeep Singh Apr 10 '17 at 10:21
  • @JagdeepSingh i already did – shiladitya Apr 10 '17 at 10:24
  • What I have pasted is working for me locally, wanted to know if it is a concern in a production environment with multiple application servers – shiladitya Apr 10 '17 at 10:24
  • 1
    @shiladitya To answer your question: No, the `:after_save` callback will only trigger in the process that triggers the `save`/`update`. – Wukerplank Apr 10 '17 at 11:21

0 Answers0