我看到这个警告并决定修复它。
弃用警告:使用关键字参数定义枚举是 已弃用并将在 Rails 8.0 中删除。位置参数 应该改用
更改我的枚举如下:
之前:
enum budget: {
below_100k: "below_100k",
from_100k_to_250k: "from_100k_to_250k",
from_250k_to_500k: "from_250k_to_500k",
from_500k_to_1m: "from_500k_to_1m",
from_1m_to_2500k: "from_1m_to_2500k",
from_2500k_to_5m: "from_2500k_to_5m",
from_5m_to_10m: "from_5m_to_10m",
from_10m_to_20m: "from_10m_to_20m",
from_20m_to_50m: "from_20m_to_50m",
from_50m_to_100m: "from_50m_to_100m",
from_100m_to_250m: "from_100m_to_250m",
from_250m_to_500m: "from_250m_to_500m",
from_500m_to_1b: "from_500m_to_1b",
from_1b_to_2500m: "from_1b_to_2500m",
from_2500m_to_5b: "from_2500m_to_5b",
from_5b_to_10b: "from_5b_to_10b",
above_10b: "above_10b",
}
之后:
enum :budget, [
:below_100k,
:from_100k_to_250k,
:from_250k_to_500k,
:from_500k_to_1m,
:from_1m_to_2500k,
:from_2500k_to_5m,
:from_5m_to_10m,
:from_10m_to_20m,
:from_20m_to_50m,
:from_50m_to_100m,
:from_100m_to_250m,
:from_250m_to_500m,
:from_500m_to_1b,
:from_1b_to_2500m,
:from_2500m_to_5b,
:from_5b_to_10b,
:above_10b
]
然后在 Rails 控制台中,我尝试了:
Organization.create!(budget: "below_100k", name: "r1")
错误:
枚举organization_budget的输入值无效:“0”
架构如下:
create_enum "organization_budget",
[
"below_100k", "from_100k_to_250k", "from_250k_to_500k", "from_500k_to_1m", "from_1m_to_2500k",
"from_2500k_to_5m", "from_5m_to_10m", "from_10m_to_20m", "from_20m_to_50m", "from_50m_to_100m",
"from_100m_to_250m", "from_250m_to_500m", "from_500m_to_1b", "from_1b_to_2500m",
"from_2500m_to_5b", "from_5b_to_10b", "above_10b"
]
这是 postgresql 查询:
SELECT enumlabel
FROM pg_enum
WHERE enumtypid = (
SELECT oid FROM pg_type WHERE typname = 'organization_budget'
);
enumlabel
-------------------
below_100k
from_100k_to_250k
from_250k_to_500k
from_500k_to_1m
from_1m_to_2500k
from_2500k_to_5m
from_5m_to_10m
from_10m_to_20m
from_20m_to_50m
from_50m_to_100m
from_100m_to_250m
from_250m_to_500m
from_500m_to_1b
from_1b_to_2500m
from_2500m_to_5b
from_5b_to_10b
above_10b
(17 rows)
我该如何解决这个问题?
gem "rails", ">= 7.2.2"
这是使用关键字参数调用
enum
:
enum symbol: { ... }
# ^^^^^^^keyword
这是使用位置参数调用
enum
:
enum :symbol, { ... }
所以你想切换到这个:
enum :budget, {
below_100k: "below_100k",
from_100k_to_250k: "from_100k_to_250k",
...
}