DEV Community

@kon_yu
@kon_yu

Posted on

How to change the table used in DelayedJob

DelayedJob, a gem that realizes asynchronous processing, uses RDMS such as MySQL or PostgreSQL for queuing (although it seems that MongoDB can also be used).

The table of this queued database is fixed to delayed_jobs by default.
In this article, I'll show you how to change this table from the default.

This measure may be used in the case of multi-tenant use of monolithic Rails.

About each gem version

  • rails: 5.1.4.
  • delayed_job: 4.1.3
  • delayed_job_active_record: 4.1.2 * rails: 5.1.4 * delayed_job: 4.1.3

Fixes config/application.rb

Change Delayed::Backend::ActiveRecord::Job.table_name in config.after_initialize block.

For example, if you want to change the default table delayed_jobs to other_delayed_jobs in DelayedJob

Module ApplicationName
  class Application < Rails::Application
    config.after_initialize do
      Delayed::Backend::ActiveRecord::Job.table_name = 'another_delayed_jobs'
    end
  end
end

DelayedJob, a gem that realizes asynchronous processing, uses RDMS such as MySQL or PostgreSQL for queuing (although it seems that MongoDB can also be used).

The table of this queued database is fixed to delayed_jobs by default.
In this article, I'll show you how to change this table from the default.

This measure may be used in the case of multi-tenant use of monolithic Rails.

About each gem version

  • rails: 5.1.4.
  • delayed_job: 4.1.3
  • delayed_job_active_record: 4.1.2 * rails: 5.1.4 * delayed_job: 4.1.3

Fixes config/application.rb

Change Delayed::Backend::ActiveRecord::Job.table_name in config.after_initialize block.

For example, if you want to change the default table delayed_jobs to other_delayed_jobs in DelayedJob

Module ApplicationName
  class Application < Rails::Application
    config.after_initialize do
      Delayed::Backend::ActiveRecord::Job.table_name = 'another_delayed_jobs'
    end
  end
end

【Reference】When using non-conventional tables in a normal ActiveRecord model

Changing the value in self.table_name

For example, in the User model, if you want to change the users table from the users table as per the Rails convention to the foo_bar_users table
I'll do it for you as follows

class User < ApplicationRecord
  self.table_name = 'foo_bar_users'
end

【Reference】When using non-conventional tables in a normal ActiveRecord model

Changing the value in self.table_name

For example, in the User model, if you want to change the users table from the users table as per the Rails convention to the foo_bar_users table
I'll do it for you as follows

class User < ApplicationRecord
  self.table_name = 'foo_bar_users'
end

Top comments (0)