Rails conforms to destroy_association_async_job config when destroying asynchronously

Saeloun Logo

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.
that was not being adhered to!


The default job that destroys
all dependencies is ActiveRecord::DestroyAssociationAsyncJob.
It does a great job out of the box,
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

class Comment < ApplicationRecord
  belongs_to :blog

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) ... 


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) ... 

Source link

Leave a reply

Please enter your comment!
Please enter your name here