gpt4 book ai didi

ruby-on-rails - ActiveRecord枚举错误: “is not a valid value”

转载 作者:行者123 更新时间:2023-12-04 13:47:20 25 4
gpt4 key购买 nike

我有一个带有枚举的模型:

  enum pending_users: {
pending_users_disabled: 0,
pending_users_enabled: 1
}

这是schema.rb中描述的字段的方式:
t.integer  "pending_users",             limit: 4,     default: 0, null: false

当我尝试通过带有以下参数的 Controller 对其进行更新时:
 Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "group"=>{"pending_users"=>"1"}, "commit"=>"Update Group", "id"=>"33"}

我看到以下错误:
ArgumentError - '1' is not a valid pending_users:
activerecord (4.2.7.1) lib/active_record/enum.rb:104:in `block (3 levels) in enum'
activerecord (4.2.7.1) lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
activerecord (4.2.7.1) lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
actionpack (4.2.7.1) lib/action_controller/metal/strong_parameters.rb:185:in `each_pair'
activerecord (4.2.7.1) lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
activerecord (4.2.7.1) lib/active_record/persistence.rb:251:in `block in update'
activerecord (4.2.7.1) lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
activerecord (4.2.7.1) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
activerecord (4.2.7.1) lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
activerecord (4.2.7.1) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
activerecord (4.2.7.1) lib/active_record/transactions.rb:220:in `transaction'
activerecord (4.2.7.1) lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
activerecord (4.2.7.1) lib/active_record/persistence.rb:250:in `update'
app/controllers/groups_controller.rb:53:in `update'

在我看来,1是枚举的有效值。什么会导致这种行为?

最佳答案

您应该为枚举分配一个字符串(键)。因此,1应该改为"pending_users_enabled"

关于ruby-on-rails - ActiveRecord枚举错误: “is not a valid value” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39279239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com