Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Activerecord brainspec/enumerize为默认值抛出mysql错误_Activerecord_Ruby On Rails 4_Enumeration - Fatal编程技术网

Activerecord brainspec/enumerize为默认值抛出mysql错误

Activerecord brainspec/enumerize为默认值抛出mysql错误,activerecord,ruby-on-rails-4,enumeration,Activerecord,Ruby On Rails 4,Enumeration,我正在开发Rails 4应用程序,并开始使用brainspec/enumerize gem。我在数据库的列status中有一个整数值,我想在我的模型中枚举它 下面您可以看到我用来设置它的代码段。不幸的是,在我的测试(之前都通过了)中,它抱怨由于无法保存行而创建了合作伙伴。无法将默认状态分配为NULL。不确定从何处获取NULL,因为数据库本身(MySQL)设置为默认值0,从下面可以看到,后端指示默认值4或:complete enumerize :status, in: [ :pending

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

下面您可以看到我用来设置它的代码段。不幸的是,在我的测试(之前都通过了)中,它抱怨由于无法保存行而创建了
合作伙伴。无法将默认状态分配为
NULL
。不确定从何处获取
NULL
,因为数据库本身(MySQL)设置为默认值
0
,从下面可以看到,后端指示默认值
4
:complete

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>'

根据brainspec/enumerize自述文件,您应该为每个状态提供一个整数值,如:

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

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