Search code examples
ruby-on-railsrubymongoiddelayed-job

Delayed Job - Find by object id


I have using Rails + DelayedJob Mongoid. I have this model:

class User
  def run
    ...
  end
end

I create new Dejayed::Job object:

user = Use.create
user.delay.run

How I can I do something like this?

Delajed::Job.where(object_id: user.id)

Solution

  • Based on @aelor answer, finally I had to add field object_id to Job class:

    # config/initializers/dejayed_job.rb
    
    module Delayed
      module Backend
        module Mongoid
          class Job
            include ::Mongoid::Document
            field :object_id
          end
        end
      end
    end
    

    Then it works ok:

    user = Use.create
    user.delay(object_id: user.id).run
    
    Delayed::Job.where(object_id: 5).first
    => #<Delayed::Backend::Mongoid::Job _id: 551a5116726a750e08000000