Answer the question
In order to leave comments, you need to log in
How to call Mongoid model from Sidekiq?
Is it possible with Mongoid to write to MongoDB from a Sidekiq worker?
# lib/worker.rb
require 'sidekiq'
require 'model' # lib/model.rb
class AwesomeWorker
include Sidekiq::Worker
def perform
RandomNumberModel.create! { random_number: Random.new.rand(1..100) }
end
end
uninitialized constant AwesomeWorker::RandomNumberModel
Answer the question
In order to leave comments, you need to log in
workaround:
class AwesomeWorker
def perform model_id
model = ObjectSpace._id2ref(model_id.to_i)
model.update_attributes { random_number: Random.new.rand(1..100) }
end
end
AwesomeWorker.perform_async AwesomeModel.new.object_id
class AwesomeWorker
def perform
::AwesomeModel.create! { random_number: Random.new.rand(1..100) }
end
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question