我正在开发一个Rails 4应用程序,我已经开始使用brainspec / enumerize gem了。我在数据库中有一个整数值,列status
,并希望在我的模型中枚举它。
下面你可以看到我用来设置它的代码片段。不幸的是,在我的测试中(之前都已通过),由于无法保存行,因此抱怨创建Partner
。无法指定默认状态NULL
。不知道它来自NULL
的位置,因为数据库本身(MySQL)设置为默认值0
,从下面可以看出,后端端指示默认值为{ {1}}或4
。
:incomplete
这是ActiveRecord / MySQL错误。
enumerize :status, in: [
:pending, # 0 account has a pending billing request (but is not yet open)
:active, # 1 account has an active base subscription
:suspended, # 2 account has been suspended (e.g. after a base subscription decline)
:expired, # 3 base subscription has expired
:incomplete, # 4 partner application process incomplete
:closed, # 5 account has been permanently closed
:cancelled # 6 account has been cancelled by user (but is still unexpired)
], default: :incomplete
此外,我知道Enumerize正在接收默认值(PartnerTest#test_create_with_nested_attributes:
ActiveRecord::StatementInvalid: Mysql2::Error: Column 'status' cannot be null: UPDATE `partner` SET `primary_contact_id` = 3, `status` = NULL WHERE `partner`.`id` = 3
test/models/partner_test.rb:9:in `block in <class:PartnerTest>'
)。如果我把乱码扔进默认值(:incomplete
),它就会被打包。
我正在使用master / branch,因此它适用于Rails 4。
的Gemfile
default: :asdoiasoas
答案 0 :(得分:0)
根据brainspec / enumerize README,您应该为每个状态提供一个整数值,例如:
enumerize :status, in: {
pending: 0, # 0 account has a pending billing request (but is not yet open)
active: 1, # 1 account has an active base subscription
suspended: 2, # 2 account has been suspended (e.g. after a base subscription decline)
expired: 3, # 3 base subscription has expired
incomplete: 4 # 4 partner application process incomplete
# And so on...
}, default: :incomplete
由于您只提供了键而不是值,因此将其设置为nil / NULL。