[Enum] Let's improve the readability of data by using rails enum

【goal】

Use of enums in rails

画面収録 2020-06-19 19.28.08.mov.gif

【merit】

■ Improved readability of db, easy to manage ■ Application is also effective for Japanese localization

【Development environment】

■ Mac OS catalina ■ Ruby on Rails (5.2.4.2) ■ Virtual Box:6.1 ■ Vagrant: 2.2.7

【Implementation】

It's very easy to say !!!! Just add to the target model

model/hoge.rb


① enum column name:[:Any word, :Any word, :Any word, :Any word]
② enum column name:{"Any word": 1,"Any word": 2.......}

hoge/_form.html.erb


<%= form.select :Column name,Model name.Column names.keys, :selected=>Model name.Column names[Column name] %>

that's all

[I want to read it together]

■ About enum https://web-camp.io/magazine/archives/16862

■ About form_with https://qiita.com/tanaka-yu3/items/50f54f5d4f4b8dfe19f3

Recommended Posts

[Enum] Let's improve the readability of data by using rails enum
Rails6: Input the initial data of ActionText using seed
[Rails] Register by attribute of the same model using Devise
[Rails] Temporary retention of data by session
The contents of the data saved by CarrierWave.
[Rails] How to handle data using enum
Get the value of enum saved in DB by Rails with attribute_before_type_cast
[Order method] Set the order of data in Rails
[Rails] Display of multi-layered data using select boxes
A review of the code used by rails beginners
Try using the query attribute of Ruby on Rails
[Rails] Sort the post list by date or number of likes using the pull-down box
When using the constructor of Java's Date class, the date advances by 1900.
Let's summarize how to extend the expiration date of Rails
[Rails] How to display the list of posts by category
[Rails] How to convert the URI of the image sent by http to https when using Twitter API
[Rails] Suppress unnecessary SQL by utilizing the cache control of the association
Replace preview by uploading by clicking the image in file_field of Rails
[Hidden_field] Let's send information using rails hidden_field !!!!
[Rails] Introduction of Rubocop by beginners
[Rails] Check the contents of the object
Explanation of the order of rails routes
Check the migration status of rails
Filter the fluctuations of raw data
[Rails 6] destroy using the resources method
[Kotlin] Example of processing using Enum
[Rails] I will explain the implementation procedure of the follow function using form_with.
I measured the performance of the in-memory data grid Apache Ignite using Yardstick.
[Ruby On Rails] How to search the contents of params using include?
How to make the schema of the URL generated by Rails URL helper https
[Ruby on Rails] Introduction of initial data
Try using || instead of the ternary operator
Let's specify the version when using docker
Rails enum Select prefecture by pull-down method
Test the integrity of the aggregation using ArchUnit ②
The identity of params [: id] in rails
Try using the Rails API (zip code)
Let's summarize "MVC" of Ruby on Rails
part of the syntax of ruby ​​on rails
The process of understanding Gemfile by non-engineers
[Swift] Termination of the program by assertion
Let's improve our business! Table of contents
[Rails] Change the label name of f.label
What is the data structure of ActionText?
Let's analyze the GC log using GCMV
The process of introducing Vuetify to Rails
Test the integrity of the aggregation using ArchUnit ③
[Rails 6] Change redirect destination at the time of new registration / login by devise
Let's improve the operation a little by monitoring and notifying the multi-line log by keyword
Sample code for search using QBE (Query by Example) of Spring Data JPA
How to make a unique combination of data in the rails intermediate table
[Rails 6 + Action Mailbox] Those who extracted the plain text of Gmail received by Action Mailbox
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)