LoginSignup
0
0

More than 3 years have passed since last update.

Enumの設定ミス。なぜかEnum指定の列からデータが取れない…

Posted at

発生した背景

※備忘録として記録します(ネタ的にはしょぼいです…)
以下のようなモデルにEnum設定したとき、データが取り出せない。

table.rb
class ColorManage < ApplicationRecord
  enum color_type: { single: 0, double: 1, graphic: 2 }
(省略)

(前提)singleというラジオボタンを押すと、0が入力されるようにする。

color-edit.html.erb
  <!--  カラータイプ、どの設定にするか。-->
  <%= color_manage_record.radio_button :color_type, :single ,class:"color-form__button" %>
  <%= color_manage_record.label :color_type, "single-color", {class: "color-form__tag"} %>

問題点> 画像のとおり、color_typeには"ゼロ"で入力されていますが、モデル抽出して、列値を取得しようとしてもnullで返ってくる

image.png

環境

項目 内容
OS.Catalina v10.15.4
Ruby v2.5.1
Ruby On Rails v5.2.4.3
MySQL V5.6

対応手順

原因)テーブルの定義がstringだったので、enumを以下のように定義しなおしました。ポートフォリオなので、恥ずかしながら、設計側を変えた次第です。

test.rb
  enum color_type: { single: "single", double: "double", graphic: "graphic" }

これで解決しました。

以上です。

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