简体   繁体   English

Rails枚举在Sidekiq作业中返回整数值

[英]Rails enum returning integer value in Sidekiq Job

We receive error reports with our enum types only when run in Sidekiq jobs. 仅当在Sidekiq作业中运行时,我们才会收到带有枚举类型的错误报告。 I'm not sure if it's for every run or random runs, but we'll commonly get errors like these: 我不确定是每次运行还是随机运行,但是我们通常会遇到如下错误:

NoMethodError: undefined method `to_sym' for 1:Integer Did you mean? NoMethodError:1:整数的未定义方法“ to_sym”是什么意思? to_s to_s

enum status: [:active, :inactive, :deleted]

def set_status
  return if self.status.to_sym == :deleted
  ...
end

Adding as an answer, so this question can be marked as closed: 添加为答案,因此此问题可以标记为已结束:

return if self.status.to_sym == :deleted should be return if deleted? return if self.status.to_sym == :deleted则应return if deleted? . You should use the magic methods provided by the enum, not work directly with the field. 您应该使用枚举提供的魔术方法,而不是直接与该领域合作。

Sidekiq is a processor for Ruby, so introducing this would create an unncessary dependency on Rails. Sidekiq是Ruby的处理器,因此引入它会在Rails上创建不必要的依赖关系。

Of course this is my opinion, but after thinking through this Rails should be the one handling numbers equivalently with their respective symbolized forms. 当然,这是我的意见,但是经过仔细考虑后,Rails应该是与它们各自的符号形式等效的一个处理数字。 For example, you can set status to 0, :active, or 'active', and it saves appropriately but the value returned during get should be consistent or methods provided to ensure consistency. 例如,您可以将status设置为0,:active或'active',并进行适当保存,但是get期间返回的值应保持一致或提供确保一致性的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM