rails delayed_job not finding method

55 Views Asked by At

Using

delayed_job_active_record  4.1
rails 6.1.1

a method within a cartitems controller correctly calls and executes a method

set_qr_code(@cartitem.cart)

But being resource intensive, delayed_job should allow to complete the parent method and run the longer method on its own. However, if the method is run with the delay verb

@cartitem.cart.delay.set_qr_code(@cartitem.cart)

The log show the complaint:

undefined method `set_qr_code' for #<Cart id: 64, ...

So although the method is cast in the application_controller.rb it is not being invoked.

The documentation has no explicit indications as to where such methods should be set. Where should one define them?

1

There are 1 best solutions below

0
cadair On

@AbM is correct. You are calling the set_qr_code method on an instance of your Cart class so it needs to go in your Cart model, not the Carts Controller.

You can call @cartitem.cart.delay.set_qr_code without the @cartitem.cart argument from your controller action, then reference the instance as self in your model:

def set_qr_code
    self.do_something_here
end

The extra call to set_qr_code(@cartitem.cart) in your CartItems controller isn't strictly necessary unless you there are additional steps you want to perform there before triggering the delayed job call.