gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:04:26 28 4
gpt4 key购买 nike

我的 Rails 4 应用程序中有几个 sidekiq worker,但无法弄清楚进程失败的原因。我将 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 initalizer 中放置一个 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com