ruby-on-rails - Rails Sidekiq 错误 : can't find object

标签 ruby-on-rails redis mongoid sidekiq

我的 Rails 4 应用程序中有几个 sidekiq 工作人员,但无法弄清楚进程失败的原因。我将 User_id 作为字符串传递给工作人员,但它似乎找不到用户。但是当我在控制台中搜索相同的 ID 时,它会找到该用户。我究竟做错了什么?这是我的代码。

Controller .rb

  def update
   @user = current_user
    if @user.bill.update_attributes(bill_params)
      RandomWorker.perform_async(@user.id.to_s)
      redirect_to users_dashboard_path, :notice => "Payment Informaton Updated"
    else
      ...  
    end
  end

随机 worker .rb

  def perform(user_id)
    user_id = user_id["$oid"] unless user_id.is_a?(String)
    user = User.find(user_id)
    user.process.update_report 
    user.process.save
  end

我的错误返回为

RandomWorker  "575a..." Mongoid::Errors::DocumentNotFound: message: Document(s) not found for class User with id(s) 575a.... summary: When calling User.find with an id or array of ids, each parameter...

--编辑--

我的数据库配置文件如下所示:

development:
 adapter: sqlite3
 database: db/development.sqlite3
 pool: 5
 timeout: 5000

production:
 adapter: sqlite3
 database: db/production.sqlite3
 pool: 25
 timeout: 5000

还有我的 mongoid.yml

development:
   # Configure available database clients. (required)
     clients:
   # Defines the default client. (required)
       default:
   # Defines the name of the default database that Mongoid can connect to.
   # (required).
         database: development_db
   # Provides the hosts the default client can connect to. Must be an array
   # of host:port pairs. (required)
         hosts:
            - localhost:27017
         options:

最佳答案

我找到了解决方案。我必须在 sidekiq 初始化程序中放置一个 Monkeypatch,以将 user.id 视为 json。显然,mongoid 与 sidekiq 一起努力解决这个问题,尽管我努力寻找一些相关文档,但我在另一个不相关的问题中偶然发现了答案。 Have to_json return a mongoid as a string

我在初始化器中添加了这个,它似乎已经解决了问题

class BSON::ObjectId
  def as_json
    self.to_s
  end
end

关于ruby-on-rails - Rails Sidekiq 错误 : can't find object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37901060/

相关文章:

ruby-on-rails - 将 Rspec 默认 GET 请求格式设置为 JSON

ubuntu - Redis不在后台运行

redis - 如何查看遗漏了哪些键?

redis - Redis 查询数

ruby-on-rails - 如何不截断 Mongoid 日志?

ruby-on-rails - 如何在 Rails 3 中编写动态路由?

sql - Rails 如何从指向同一个表的联合关联中进行选择

javascript - Rails PDFKit 等待客户端

ruby-on-rails - 在 mongoid-gridfs 上使用 Rails 进行 Cucumber 测试时出现“ERROR Mongo::OperationFailure”

ruby-on-rails - Mongoid:一次调用创建多个对象