LoginSignup
0
0

More than 3 years have passed since last update.

enumを導入すると起きるエラー【rails】

Posted at

概要

数字を文字列に変換してくれる最強助っ人「enum」ちゃんを導入するとエラー発生。

エラー文
'x'ArgumentError in xxx
'x' is not a valid xxx

スクリーンショット 2020-01-09 18.22.23.png

解決方法

Shift_user.model(enumを書いているモデル)での定義ミス。

詳細

Before↓

model.rb
class ShiftUser < ApplicationRecord

enum work_type:{
"午前": 1,
"午後": 2,
"一日": 3,
}
end

After↓

model.rb
class ShiftUser < ApplicationRecord
  enum work_type: {
    am: 1,
    pm: 2,
    all_day: 3
  }
  # 英語から日本語
  WORK_TYPE = {
    am: '午前',
    pm: '午後',
    all_day: '1日'
  }

そもそもenumで日本語でも指定はできるが、
よく使われるのが「アルファベット or _」を使って命名するらしい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0