如何通过Ruby on Rails将current_user对象传递给Sidekiq Worker?

更新于
2026-10-09 18:44:51
1阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计382个文字,预计阅读时间需要2分钟。

如何通过Ruby on Rails将current_user对象传递给Sidekiq Worker?

在伪创新的开头内容中,可以简化为:

我在测试代码时,尝试将 `current_user` 或 `User.find(1)` 传递给工作模型模块,但在 Sidekiq 的代理模板中获取时出现错误(localhost:3000/sidekiq/retries):`NoMethodError: undefined method 'supports?' for `

这样的输出简洁明了,不超过100个字,直接指出了问题所在。

我试图将current_user或User.find(1)传递给工作模块,但在sidekiq的仪表板中获取错误(localhost:3000 / sidekiq / retries):

NoMethodError: undefined method `supports’ for “#”:String

注意:我的关系很好,即:

u = User.find(1) u.supports #=> []

supports_controller.rb:

def create @user = current_user ProjectsWorker.perform_async(@user) ... end

应用程序/工人/ projects_worker.rb:

class ProjectsWorker include Sidekiq::Worker def perform(user) u = user @support = u.supports.build(support_params) end end

重新启动我的sidekiq服务器没有任何区别.这是在我的开发机器上.

从 Sidekiq documentation:

The arguments you pass to perform_async must be composed of simple
JSON datatypes: string, integer, float, boolean, null, array and hash.
The Sidekiq client API uses JSON.dump to send the data to Redis. The
Sidekiq server pulls that JSON data from Redis and uses JSON.load to
convert the data back into Ruby types to pass to your perform method.
Don’t pass symbols or complex Ruby objects (like Date or Time!) as
those will not survive the dump/load round trip correctly.

传递id而不是object:

def create ProjectsWorker.perform_async(current_user.id) end

工人:

如何通过Ruby on Rails将current_user对象传递给Sidekiq Worker?

class ProjectsWorker include Sidekiq::Worker def perform(user_id) u = User.find(user_id) @support = u.supports.build(support_params) end end

本文共计382个文字,预计阅读时间需要2分钟。

如何通过Ruby on Rails将current_user对象传递给Sidekiq Worker?

在伪创新的开头内容中,可以简化为:

我在测试代码时,尝试将 `current_user` 或 `User.find(1)` 传递给工作模型模块,但在 Sidekiq 的代理模板中获取时出现错误(localhost:3000/sidekiq/retries):`NoMethodError: undefined method 'supports?' for `

这样的输出简洁明了,不超过100个字,直接指出了问题所在。

我试图将current_user或User.find(1)传递给工作模块,但在sidekiq的仪表板中获取错误(localhost:3000 / sidekiq / retries):

NoMethodError: undefined method `supports’ for “#”:String

注意:我的关系很好,即:

u = User.find(1) u.supports #=> []

supports_controller.rb:

def create @user = current_user ProjectsWorker.perform_async(@user) ... end

应用程序/工人/ projects_worker.rb:

class ProjectsWorker include Sidekiq::Worker def perform(user) u = user @support = u.supports.build(support_params) end end

重新启动我的sidekiq服务器没有任何区别.这是在我的开发机器上.

从 Sidekiq documentation:

The arguments you pass to perform_async must be composed of simple
JSON datatypes: string, integer, float, boolean, null, array and hash.
The Sidekiq client API uses JSON.dump to send the data to Redis. The
Sidekiq server pulls that JSON data from Redis and uses JSON.load to
convert the data back into Ruby types to pass to your perform method.
Don’t pass symbols or complex Ruby objects (like Date or Time!) as
those will not survive the dump/load round trip correctly.

传递id而不是object:

def create ProjectsWorker.perform_async(current_user.id) end

工人:

如何通过Ruby on Rails将current_user对象传递给Sidekiq Worker?

class ProjectsWorker include Sidekiq::Worker def perform(user_id) u = User.find(user_id) @support = u.supports.build(support_params) end end