title: How to access Delayed Job instance inside Active Job - Rails 4.2
body: <p>I'm using ActiveJob with delayed_job (4.0.6) in the background and I want to find a scheduled job to deleted it.</p>
  
  <p>For instance, if I have</p>
  
  <pre><code>class MyClass
  
    def my_method
      perform_stuff
      MyJob.set(wait: 1.month.from_now).perform_later(current_user)
    end
  end
  </code></pre>
  
  <p>Then, if I edit MyClass instance and call my_method again, I want to cancel that job and schedule a new one.</p>
  
  <p>As suggested in this post <a href="http://www.sitepoint.com/delayed-jobs-best-practices/" rel="nofollow">http://www.sitepoint.com/delayed-jobs-best-practices</a>, I added two columns to the Delayed Job Table:</p>
  
  <pre><code>table.integer :delayed_reference_id
  table.string  :delayed_reference_type
  
  add_index :delayed_jobs, [:delayed_reference_id],   :name =&gt; 'delayed_jobs_delayed_reference_id'
  add_index :delayed_jobs, [:delayed_reference_type], :name =&gt; 'delayed_jobs_delayed_reference_type'
  </code></pre>
  
  <p>So this way I may find a delayed Job and destroy it. But I wanted to do that inside a ActiveJob class, to maintain the pattern of jobs in my project.</p>
  
  <p>I wanted to do something like:</p>
  
  <pre><code>class MyJob &lt; ActiveJob::Base
  
    after_enqueue do |job|
      user = self.arguments.first
      job.delayed_reference_id = user.id,
      job.delayed_reference_type = "User" 
    end
  
    def perform(user)
      delete_previous_job_if_exists(user_id)
    end
  
    def delete_previous_job_if_exists(user_id)
      Delayed::Job.find_by(delayed_reference_id: 1, delayed_reference_type: 'User').delete
    end
  end
  </code></pre>
  
  <p>But that doesn't work.</p>
  
  <p>Anyone had this kind of issue?</p>