[RUBY] [Rails] How to use enum

Introduction

I tried using enum instead of Active Hash in application development, so I summarized it lightly.

table of contents

  1. Table settings
  2. Model settings
  3. View file settings

1. Table settings

This time we will use enum for the column called gender. When using enum, the column type must be ** integer type **.

Column name Column type
gender integer

2. Model settings

Describe the data in the model file. The enum is defined in the form of a hash like the one below.

Example
enum gender: { man: 0, woman: 1, other: 2 }

3. View file settings

This time it was displayed as a radio button. Describe the column name in the first argument and the value defined in the model in the second argument.

<div class="field">
  <%= f.label :man %>
  <%= f.radio_button :gender, :man %>
  <%= f.label :woman %>
  <%= f.radio_button :gender, :woman %>
  <%= f.label :other %>
  <%= f.radio_button :gender, :other %>
</div>
Flow of saving to database when man is selected

Send man as key in view → Controller → Find number based on key sent in model → Save as numerical value in database

Reference link

https://madogiwa0124.hatenablog.com/entry/2017/12/24/222156

Recommended Posts

[Rails] How to use enum
[Rails] How to use enum
How to use rails join
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] How to use flash messages
How to use Ruby on Rails
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use Java enum type
How to use Map
How to use custom helpers in rails
How to write Rails
How to use rbenv
[Ruby on Rails] How to use CarrierWave
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use map
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use MySQL in Rails tutorial
How to use hidden_field_tag
How to use TreeSet
How to uninstall Rails
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Ruby on Rails] How to use kaminari
[Rails] Enum is easier to use! Enumelize!
How to use Dozer.mapper
How to use Gradle
[Rails] How to use video_tag to display videos
[Rails] How to use helper method, confimartion
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] How to handle data using enum
How to use Map
[Rails] How to use select boxes in Ransack
How to use enum (introduction of Japanese notation)
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
[Ruby on Rails] How to use session method
[Rails] How to use PostgreSQL in Vagrant environment
[Java] How to use Map
[rails] How to post images
How to use Chain API
[Java] How to use Map