[RUBY] [Rails] How to use enum

Introduction

How to use Enum in Ruby and Rails A memo to remember.

What is an enum?

"Enum" means "enumeration type". -DB is registered as int type or boolean type, and any key name associated with the numerical value can be given. -In the source code, the associated key name is used.

advantage -If the data type is set to integer, it will be registered as a number in the DB, but if it is only a number, the meaning of the number cannot be shared. -If you define it as a character string, there is a high possibility of typing, but if you use an enum, you do not have to worry about it. ・ Easy to correct.

Definition method

You need to define an enum for your model. There are two definition methods. ** 1. Definition only ** ** 2. Specify a numerical value in the definition **

Specify only the definition

Define with an array. They are linked in order from 0 in the order of definition. In the example below, yesterday → 0, today → 1, tomorrow → 2 are automatically sorted. enum column name: Specify enum with [: key name you want to give].

** * [] Nanoto: Pay attention to the position **

Model file


enum column name:    [ :yesterday, :today, :tomorrow]
↑ What to define

Specify a numerical value in the definition

enum Column name: {Key name you want to add: Corresponding number} Specify enum. ** * {} and: Pay attention to the position **

Model file


enum column name:    {yesterday: 0,today: 1,tomorrow: 2 }
↑ What to define

Use in view

You can create a select box with the description below. ** Column name and multiple column types are the same column and the column described after enum ** ** The model name is the model described at the time of definition **

view file


 <%= select :Column name, :name,Model name.Column name複数形.keys.to_a, include_blank: true %>

At the end

・ I used it when changing the status or when the status was changed in conjunction with it. ・ When comparing with if etc. on the controller, I think that you can compare with defined characters instead of numbers. Example) if Information you want to compare == "Yesterday" Example) Information you want to find .where (column name: "today") .count etc.

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 java.util.logging
[Rails] How to use rails console with docker
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 rails g scaffold, functions, precautions
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