ruby-on-rails-4 – 枚举选项“新”无效

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-4 – 枚举选项“新”无效前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在我的模型上创建一个枚举,我希望其中一个状态是“新的”

例如

enum status: { stale: 0,new: 1,converted: 2 }

似乎rails拒绝这个以下错误.

You tried to define an enum named "status" on the model "Lead",but this will generate a class method "new",which is already defined by Active Record.

我理解为什么会这样,但我想知道是否有办法解决这个问题?

解决方法

错误清楚地表明您不能使用新密钥枚举,因为它将与现有的ActiveRecord方法冲突.没有办法解决这个问题.

这个问题并不新鲜,之前已经讨论过了.

我建议你阅读

enum: Option not to generate “dangerous” class methods

根据Rails合作者Godfrey Chan的说法:

In this case,if you want to use enum,you are probably better off
renaming your label to something else. This is not unique to enums – a
lot of Active Record features generates methods for you and usually
there aren’t ways to opt-out of those generated methods.

Gonna give this one a close for now….

原文链接:https://www.f2er.com/ruby/268557.html

猜你在找的Ruby相关文章