ActiveJob Vs ApplicationJob

A very quick clarification on ActiveJob and ApplicationJob

If you are looking up documentation about the Jobs functionality within Rails you can occasionally see that the sample Jobs class appears to inherit from two different classes.

Some examples will show something like;

class MyJob < ActiveJob::Base
end

Whilst others will be more like;

class MyJob < ApplicationJob
end

When you create a new Rails 5 project a file is created within app/jobs called application_job.rb and in it we create a class called ApplicationJob which inherits from ActiveJob::Base.

class ApplicationJob < ActiveJob::Base
end

The chances are if you’re reading a guide that mentions ApplicationJob then it was created assuming a Rails 5 starting point, guides or sample code mentioning ActiveJob::Base are ones that don’t assume you have the application_job.rb file.

Best practice is to use ApplicationJob and create the empty class if you don’t have one already – this will allow you to add code which can be inherited by all of your Jobs.


Recent posts View all

Ruby

Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails

Writing Marketing

We've deleted an article's worth of unhelpful words

We've improved several pages across our site by removing words that add no value, and often detract from the article.