我有一个带有该枚举的模型:
enum food: { not_food: 0, is_food: 1 }
但是它似乎无法按预期工作:
2.4.4 :007 > Analyte.where( food: 'is_food').count
(3.0ms) SELECT COUNT(*) FROM "ANALYTES" WHERE "ANALYTES"."FOOD" = :a1 [["food", 0]]
=> 12709
2.4.4 :008 > Analyte.where( food: 'not_food').count
(3.1ms) SELECT COUNT(*) FROM "ANALYTES" WHERE "ANALYTES"."FOOD" = :a1 [["food", 0]]
=> 12709
请注意,在两种情况下,零都是查询的参数。
我也看到了这个https://medium.com/@elcortez/the-many-issues-with-rails-enum-36cfb2bec620 还有这个 https://github.com/rails/rails/issues/32618
我应该在项目中使用枚举还是不值得? 如果重要的话,我会使用Rails 4.2。
食物在数据库中的定义如下:
# food :integer default(0)