backgroundrb after_create February 27th, 2008

I had setup a background worker so that after a model was created it offloaded some heavier processing to a background worker so you wouldn't have to wait. I could get it to run just fine if I ran this process separately, but within the after_create it always failed.

The setup was that the after_create called the worker like so:

def after_create
  MiddleMan.ask_work(:worker => :new_model_worker, :worker_method => :init, :data => self.id)
end
I figured that after_create meant it should have been saved, everything should be hunky dory. In the logs it even said it was failing to find the model with the correct id(so id was getting set). It was driving me nuts, until it occurred to me to try and call reload in the after create before the worker call, like so:
def after_create
  reload
  MiddleMan.ask_work(:worker => :new_model_worker, :worker_method => :init, :data => self.id)
end
And suddenly it works, right as rain. I still don't know exactly why this is necessary, but it works. Hope it does for you too, and I'd love to hear an explanation in the comments.

Sorry, comments are closed for this article.