title: Rails: Triggering observer from model saved in delayed_job
body: <p>I have a model which is observed but the observer's callback isn't called when saving the model in a delayed job</p>
  
  <p>The observer:</p>
  
  <pre><code># app/models/my_model_observer.rb
  class MyModelObserver &lt; ActiveRecord::Observer
    observe MyModel
  
    def after_create(my_model)
      Rails.logger.debug my_model.inspect
    end
  end
  </code></pre>
  
  <p>I've registered the observer:</p>
  
  <pre><code>config.active_record.observers = 'MyModelObserver'
  </code></pre>
  
  <p>And in the controller where I create the model I use delayed_job:</p>
  
  <pre><code>def create
    MyModel.delay.do_lots_of_work params[:my_model]
  end
  </code></pre>
  
  <p>At the end of <code>do_lots_of_work</code> I call <code>save</code> on a new instance of my_model and it is successfully saved to the db but the observer's <code>after_create</code> callback never runs. I can tell it doesn't run because it doesn't log, I've even tried making it write to a new file in root but no file is ever created. </p>
  
  <p>Does delayed_job not notice that I've registered an observer? Is there a way to fix that? How can I observe a model's lifecycle when it happens in a worker?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I've been trying to debug this so I started by using <code>binding.pry</code> within the delayed method to see if the observer was registered. My delayed_job log shows the progress (from my own logging) up until binding.pry stops and gives me control of the console. I type <code>MyModelObserver</code> and yes its there. I type <code>MyRailsApp::Application.config.active_record</code> and yes the observer is registered. I type exit, and nope, my observer does not log anything. I try again but this time I try instantiating the observer <code>MyModelObserver.new</code> that complains saying new is a private method. Ok fine, I'll force it <code>MyModelObserver.send :new</code> and that returns an instance of the observer, great, whatever. I type exit after that and holy shit my observer logs after the model saves! What?! So, I added that instantiating line to the top of my delayed method, and it fixes the problem, my observer is now executing. But of course I don't want to hard code that here so I still have a problem. What's happening in delayed_job that's not setting up the observers correctly?</p>
  
  <p>The observer works without delaying the method and, of course, without manually instantiating the observer.</p>