0
0

More than 3 years have passed since last update.

enumで、キーの文字列に数字を含める方法

Last updated at Posted at 2019-11-15

*前提*

schema.rb
t.integer "sex"
t.integer "age"

通常のenum(文字列のみ)の書き方は以下の通り。

user.rb

enum sex: { 未選択: 0, 男性: 1, 女性: 2 }

enumで数字を含めるには以下のように

user.rb

enum age: { 未選択: 0, "10代": 1, "20代": 2, "30代": 3, "40代": 4, "50代": 5, "60代": 6, "70代": 7, "80代": 8, "90代": 9 }

キーをダブルクオーテーション("")で囲えば良い。
むしろ本来こんなふうに書くべきなのかもしれない。

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