module Attributes

Ruby on Rails edge

Since edge

Active Job Attributes

The Attributes module provides typed attributes for jobs using the Active Model Attributes API. Declared attributes are automatically serialized with the job data and restored when the job is deserialized.

This is especially useful with ActiveJob::Continuation, where a job may be interrupted and resumed multiple times and you need to persist attributes across steps until the job finishes.

class SubmitEnrollmentJob < ApplicationJob
  include ActiveJob::Continuable

  attribute :payment_token, :string
  attribute :billing_profile_id, :integer

  def perform(enrollment)
    step :tokenize_payment_instrument do
      self.payment_token = PaymentGateway.tokenize(enrollment.user.payment_instrument)
    end

    step :create_billing_profile do
      self.billing_profile_id = BillingProfileApi.create(customer_id: enrollment.user_id)
    end

    step :submit_enrollment do
      submission_id = EnrollmentApi.submit(enrollment, payment_token, billing_profile_id)
      enrollment.update!(status: 'processing', submission_id: submission_id)
    end
  end
end

Attributes also work without Continuation, persisting across retries.

Attributes support all built-in Active Model types, see ActiveModel::Attributes for details. For custom types attribute values must be serializable as Active Job arguments. See ActiveJob::Arguments for the full list of supported types.

Includes

Extends

Private methods (2)

Show private methods Implementation detail — not part of the public API.

Used by

Included by (1)

Methods (inherited)

From ActiveModel::Attributes (2)
From ActiveSupport::Concern (3)
From ActiveModel::AttributeMethods (4)
From ActiveSupport::Autoload (5)

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close