2

我正在开发 Rails 4 应用程序,并且我已经开始使用 Brainspec/enumerize gem。我在数据库列中有一个整数值,status并想在我的模型中枚举它。

您可以在下面看到我用来设置它的代码段。不幸的是,在我的测试中(之前都通过了),它抱怨Partner由于无法保存该行而创建了一个。无法分配 的默认状态NULL。不确定它NULL来自哪里,因为数据库本身(MySQL)设置为默认值,0并且从下面可以看到,后端指示默认值4or :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>'

此外,我知道:incompleteEnumerize 正在获取默认值 ( )。如果我将胡言乱语放入默认值 ( default: :asdoiasoas) 中,它就会失败。

我正在使用主/分支,以便它与 Rails 4 一起使用。

宝石文件

gem 'enumerize', :github => 'brainspec/enumerize'
4

1 回答 1

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。

于 2014-06-30T02:14:59.327 回答