uninitialized constant Delayed::Job

12,910

Solution 1

Did you follow the installation instructions on the README file? https://github.com/collectiveidea/delayed_job

Add this to your gemfile:

gem 'delayed_job_active_record'

and then run this at the console:

$ rails generate delayed_job:active_record
$ rake db:migrate

You need to create the delayed jobs table in the database (this assumes you're using active record).

For Rails 3, all you need to do is include it in the gemfile, run that code above to create the table and migrate the database, then restart your server and go!

Solution 2

If you've upgraded to delayed_job version >=3 you'll need to add this (presuming you're using ActiveRecord):

# Gemfile
gem 'delayed_job_active_record'

Solution 3

I'm using delayed job within an engine (so the gem is specified in a .gemspec rather than Gemfile) and was getting the same error. I found that I could solve the problem by using:

require 'delayed_job_active_record'  # fixes problem

rather than

require 'delayed_job'                # doesn't

Solution 4

Just in case, if this is still unanswered, check the below link

http://www.pipetodevnull.com/past/2010/4/14/uninitialized_constant_delayedjob/

edit: Alternative, just upgrade to the latest version - 2.1

Solution 5

i was struggling a while back with the same problem. i was following ryan bates screencast on delayed_job and got the same error 'uninitialized constant Delayed::Job'. In the screencast ryan creates a file called mailing_job.rb(located under lib folder) with the delayed_job perform method inside, which allows you to use the enqueue method. After doing some research i found that rails 3 does not automatically load the lib folder files into your app.(not entirely sure)

Try this
In your controller where you use this:

"Delayed::Job.enqueue do_it(), 0, 1.minutes.from_now.getutc" 

Try to require the file like this.

require 'mailing_job'
class AssetsController < ApplicationController
    def some_method
        Delayed::Job.enqueue do_it(), 0, 1.minutes.from_now.getutc
    end
end
Share:
12,910
Gerard
Author by

Gerard

I have no idea what I'm doing.

Updated on June 07, 2022

Comments

  • Gerard
    Gerard about 2 years

    I've added the delayed_job gem to my gemfile and installed correctly but when I try to run the following line:

    Delayed::Job.enqueue do_it(), 0, 1.minutes.from_now.getutc
    

    I get the error 'uninitialized constant Delayed::Job'

    Can somebody explain what i need to do here? I've tried running 'rake jobs:work' beforehand but it also returns the 'uninitialized constant Delayed::Job' error. Additionally, I've added "require 'delayed_job'" to the file (application.rb) without much luck.

  • Peter Nixey
    Peter Nixey over 12 years
    Very helpful - I had that problem. Complete aside but to save someone else hitting it I also had to update my Paperclip usage from gem 'aws-s3' to gem 'aws-sdk'
  • Cam
    Cam over 12 years
    See "Upgrading from 2.x to 3.0.0 on Active Record" here: github.com/collectiveidea/delayed_job
  • jbg
    jbg over 12 years
    This fixed my issue moving to a cedar stack on Heroku along with Peter's comment about aws. Thanks!
  • lfzawacki
    lfzawacki almost 12 years
    This solved my problem, but the link has 1 line of code, you could've easily pasted it here.
  • sorens
    sorens over 11 years
    Also make sure that you add app/models/delayed_job.rb as well. Don't need to define much, just the class itself: class DelayedJob < ActiveRecord::Base end should do it.
  • just__matt
    just__matt over 11 years
    @sorens can you confirm that is a necessary step?
  • sorens
    sorens over 11 years
    @just__matt I believe so, but I haven't confirmed it. Start a new rails project, add the delayed_job gem, skip the model and see.
  • just__matt
    just__matt over 11 years
    @sorens I am using delayed_job without having defined a DelayedJob model - I don't think it's a necessary step - I recommend against it to avoid confusion
  • sorens
    sorens over 11 years
    It sounds like it's an optional step. I write CustomJobs to encapsulate all my delayed_job functionality. Perhaps that is why it was needed. For future developers, try it without creating the model and if it works, you're off to the races!
  • ReggieB
    ReggieB almost 11 years
    Delayed::Worker.backend = :active_record
  • ReggieB
    ReggieB almost 11 years
    No it has not. The key point is that in an engine I had to specifically use a require statement. The other answers refer to standard rails apps, where using "gem 'delayed_job_active_record'" in the Gemfile was enough. This is not the case for an engine where the gem is defined in a gemspec, and you then need to use a require statement where the gem is needed in your code.
  • Alex V
    Alex V over 10 years
    Yes this require statement does it! Thank you.
  • Asciant
    Asciant about 9 years
    Thanks for the answer, this worked for me from within a Rails Engine. Incase anyone wondered like myself, the require went at the top of the engine.rb file.