我正在开发 Rails 4 应用程序,并且我已经开始使用 Brainspec/enumerize gem。我在数据库列中有一个整数值,status
并想在我的模型中枚举它。
您可以在下面看到我用来设置它的代码段。不幸的是,在我的测试中(之前都通过了),它抱怨Partner
由于无法保存该行而创建了一个。无法分配 的默认状态NULL
。不确定它NULL
来自哪里,因为数据库本身(MySQL)设置为默认值,0
并且从下面可以看到,后端指示默认值4
or :incomplete
。
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
这是 ActiveRecord/MySQL 错误。
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
Enumerize 正在获取默认值 ( )。如果我将胡言乱语放入默认值 ( default: :asdoiasoas
) 中,它就会失败。
我正在使用主/分支,以便它与 Rails 4 一起使用。
宝石文件
gem 'enumerize', :github => 'brainspec/enumerize'