Non persistent record and delayed jobs

Question!

Is there a way to add a non persistent record to delayed job? I recently updated my Rails version, but now I can't add a non persistent record to delayed job.

This is my class:

class InfoPackDownload

  include ActiveAttr::Model
  include ::NewRelic::Agent::MethodTracer

  attribute :course_id
  attribute :first_name
  attribute :last_name
  attribute :email
  attribute :telephone
  attribute :receive_newsletter, type: Boolean
  attribute :filename
  attribute :course_code
  attribute :programme_instance_code
  attribute :presentation
  attribute :title

  validates_presence_of :first_name, :last_name
  validates :email, presence: true, email: true

  def send_interaction
    body_params = create_request_body
    request = build_request body_params
    send_request request
  end
end

I am trying to add the following method to delayed job:

@info_pack_download.delay(queue: 'info_pack_download').send_interaction

But when I call this method, an error is raised for adding a non persistent record to delayed job.

Can anyone offer some advice on this problem?



Answers

Delayed job needs to store the arguments for the job to the database, so that it can retrieve them later once the job gets scheduled. If it's a simple ruby object, it can serialize it to a string (to_json), and store that. If it's a database model, it stores the ID and the class name.

When the job gets scheduled to run, it needs to pull the arguments out of the database. For the simple model, it can deserialize the string (JSON.parse), for the database model, it can load the model from the database using the class name and ID. You are not providing a way to do either.

By : Daiku


This video can help you solving your question :)
By: admin