Rails 5 - 使用枚举,当枚举值是保留字时该怎么办?

rmc*_*rry 5 enums ruby-on-rails

我有一个简单的枚举(在 Rails 4.something 中添加)

enum direction_type: [:undefined, :loop, :out_and_back, :point_to_point]
Run Code Online (Sandbox Code Playgroud)

哪个有效,但在日志中我看到这个错误:

Creating scope :loop. Overwriting existing method Trail.loop.
Run Code Online (Sandbox Code Playgroud)

另一个:

enum status: [:undefined, :new, :draft, :published]

Creating scope :new. Overwriting existing method Trail.new.
Run Code Online (Sandbox Code Playgroud)

我不完全确定这些错误意味着什么,但它们听起来不太好!

我应该更改这些枚举还是有其他解决方案(例如我可以添加一些巧妙的范围规则来修复它们?)

Bor*_*aMa 4

即使您无法更改枚举值,您仍然应该能够解决与enum prefix 或 postfix-ing 的冲突。_prefix枚举定义中的/选项_postfix应保持枚举的值不变,但会影响自动生成的布尔值和 bang 方法名称以及作用域名称

所以,像这样:

enum direction_type: [:undefined, :loop, :out_and_back, :point_to_point], _prefix: true
Run Code Online (Sandbox Code Playgroud)

应该生成以下范围:direction_type_undefineddirection_type_loop等...这不会与现有的类方法发生冲突。

有关更多信息,请参阅源代码中的文档。