Search code examples
ruby-on-railsactiverecorddelayed-job

Delayed::Job losing active record relation, but instead array


I am having a Job class for sending Email. I am passing the collection (active record relation object, lets say @payments = Payment.where(status: 'pending') to the argument of Mailer class.

Delayed::Job.enqueue(
  EmailJob.new(
    company: current_company,
    type: :payments,
    collection: @payments # Payment::ActiveRecord_Relation
  ),
  target: 'Payment Email',
  company: current_company
)

The problem I am facing is, When the worker picks the Job, the collection is considered as just an Array of payments, I am expecting this as an activerecord collection object so that I can do some stuffs over those.


Solution

  • ActiveJob supports the following types of arguments by default:

    • Basic types (NilClass, String, Integer, Float, BigDecimal, TrueClass, FalseClass)
    • Symbol
    • Date
    • Time
    • DateTime
    • ActiveSupport::TimeWithZone
    • ActiveSupport::Duration
    • Hash (Keys should be of String or Symbol type)
    • ActiveSupport::HashWithIndifferentAccess
    • Array
    • Module
    • Class

    - Rails guides: Active Job Basics

    When you pass an ActiveRecord::Relation, Rails will serialize it by casting it into an array as its not a supported type. ActiveJob does use GlobalId to pass individual records though so it seems like a better option is probally just to create an association or some other method to fetch the payments in your job given a company.

    See also Can't pass CollectionProxy object to ActiveJob .