【问题标题】:Enum active record clearance_level problems枚举 activerecord 清除级别问题
【发布时间】:2017-09-29 16:38:37
【问题描述】:

我有一个授权级别的用户表...(客户/供应商/管理员) 但我似乎无法保存默认设置为 0 的新用户,这将等于“客户”错误是说“'0'不是有效的clearance_level”?

这是我的用户模型

  class User < ApplicationRecord
    include Clearance::User
    validates :email, presence: true
    validates :password, presence: true
    validates :password, confirmation: { case_sensitive: true }
    enum clearance_level: [:customer, :vendor, :admin]
  end

这是我的迁移文件...(我删除了该列并再次添加它以防解决它?)

  class ChangeClColumns < ActiveRecord::Migration[5.1]
    def change
      remove_column :users, :clearance_level
      add_column :users, :clearance_level, :integer, :default => 0

    end
  end

也是我的架构

create_table "users", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", null: false
t.string "encrypted_password", limit: 128, null: false
t.string "confirmation_token", limit: 128
t.string "remember_token", limit: 128, null: false
t.string "first_name"
t.string "last_name"
t.date "date_of_birth"
t.integer "clearance_level", default: 0
t.index ["email"], name: "index_users_on_email"
t.index ["remember_token"], name: "index_users_on_remember_token"

结束

【问题讨论】:

    标签: ruby-on-rails activerecord enums


    【解决方案1】:

    这是因为您保存的是字符串“0”,而不是整数 0。从参数中获得的所有内容都是字符串。您在这里有两个选择: 1. 在赋值之前将其转换为整数。 2. 使用enum 名称并从您的表单中发送“客户”而不是“0”。两者都会成功保存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多