Rails conforms to destroy_association_async_job config when destroying asynchronously


Rails 6 first added the option to destroy dependencies asynchronously. It allowed cascading destroys to be offloaded into a background process for more scalable solutions.

Like all the other Rails magic, this was provided out of the box. Fortunately, a configuration option was also provided to override the default job that performs these destroys. However, that was not being adhered to!

Before

The default job that destroys all dependencies is ActiveRecord::DestroyAssociationAsyncJob. It does a great job out of the box, however, there was a bug that did not allow this job to be overridden with the provided configuration option.

Let’s look at an example.

class Blog < ApplicationRecord
  has_many :comments
end

class Comment < ApplicationRecord
  belongs_to :blog
end

Now when we set the config.active_record.destroy_association_async_job option to use CustomDestroyAssociationAsyncJob instead, it is not adhered.

> Blog.last.destroy
=> Enqueued ActiveRecord::DestroyAssociationAsyncJob (Job ID: 5c376b24-d294-4de8-ba3b-9aaebfb9ae22) to Sidekiq(default) ... 

After

Thanks to this PR it is now possible to override the default destroy association async job.

> Blog.last.destroy
=> Enqueued CustomDestroyAssociationAsyncJob (Job ID: 5c376b24-d294-4de8-ba3b-9aaebfb9ae22) to Sidekiq(default) ... 

Join Our Newsletter