我有一个简单的枚举(在 Rails 4.something 中添加)
enum direction_type: [:undefined, :loop, :out_and_back, :point_to_point]
哪个有效,但在日志中我看到了这个错误:
Creating scope :loop. Overwriting existing method Trail.loop.
另一个:
enum status: [:undefined, :new, :draft, :published]
Creating scope :new. Overwriting existing method Trail.new.
我不完全确定这些错误意味着什么,但它们听起来不太好!
我应该更改这些枚举还是有其他解决方案(例如我可以添加一些巧妙的范围规则来修复它们?)
最佳答案
即使您无法更改枚举值,您仍然应该能够解决与 的冲突。枚举前缀或后缀 . _prefix
/_postfix
枚举定义中的选项应该保持枚举的值不变,但会影响自动生成的 bool 值和 bang 方法名称 以及作用域名称 .
所以,像这样:
enum direction_type: [:undefined, :loop, :out_and_back, :point_to_point], _prefix: true
应该生成以下范围:
direction_type_undefined
, direction_type_loop
等...这不会与现有的类方法发生冲突。见docs in the source code了解更多信息。
关于ruby-on-rails - Rails 5 - 使用枚举,当枚举值是保留字时怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39397621/