2016-09-01 4 views
2

У меня есть модель с перечислением:ActiveRecord ошибка перечисление: «не является допустимым значением»

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

А вот как поле, описываемое в schema.rb:

t.integer "pending_users",    limit: 4,  default: 0, null: false 

Когда я пытаюсь обновить его через контроллер со следующими параметрами:

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

Вы должны просто использовать 'enum pending_users:% w (enabled disabled)'. Префикс 'pending_users_' является избыточным. – meagar

+0

@meagar Здесь я не включил всю модель. На самом деле у меня есть другие разрешенные/отключенные перечисления в моей модели, и в rails 4 нет способа включить несколько разных перечислений с одинаковым значением, к сожалению – borisano

ответ

6

Вы должны назначить перечисление с помощью строки (ключа). Таким образом, 1 должен быть "pending_users_enabled".

Смежные вопросы