微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/ruby/268557.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐