Search code examples
ruby-on-railsrubymessage-queuesidekiq

delete request with sidekiq


I am new to Ruby on Rails and Sidekiq. I want to set this delete request to be done in Sidekiq queue and I don't know how to send it to the perform method, I am sending the Book model to the perform method

My controller Action code

def destroy
    BaseWorkerJob.perform_async(Book)
end

My BaseWorkerJob class code

class BaseWorkerJob
   include Sidekiq::Job
   sidekiq_options retry:0

   def perform(book)
   # Do something
     book.find(params[:id]).destroy!
     sleep 15
   end
end

SideKiq Error

enter image description here

ruby 3.1.2
Rails 7.0.4


Solution

  • You can send the model name and object id to the worker

    def destroy
      BaseWorkerJob.perform_async(Book.to_s, params[:id])
    end
    
    class BaseWorkerJob
      include Sidekiq::Job
      sidekiq_options retry: 0
    
      def perform(klass_name, object_id)
        klass_name.constantize.find(object_id).destroy!
      end
    end
    

    Try it out!