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

JavaScript

Setting a more specific database type in Prisma

Prisma sets a default of TEXT for all String fields, here is how you can override that.

Ruby

Override database attribute types

Sometimes you don't have control over how your database handles information, so you need Rails to set it