[Ruby] [Enum] Use rails enum to improve data readability

less than 1 minute read

##【goal】 Using enum in rails

![Screen capture 2020-06-19 19.28.08.mov.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/620689/60c48f87-cc70-8c9e-(1088-200d92cbcaf5.gif)

##【merit】 ■ Improved readability of db and easy to manage ■ Application is effective for Japaneseization

##【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 ####!!!! Only add to the target model

  • ① is the easiest way to write ※② is also available in Japanese

model/hoge.rb


 enum column name: [:arbitrary word, :arbitrary word, :arbitrary word, :arbitrary word]
 enum column name: {"arbitrary word": 1, "arbitrary word": 2.......}

*Change number_field to select *The value set with the enum can be acquired and selected.

hoge/_form.html.erb


<%= form.select :column name, model name.column name s.keys, :selected=>model name.column name s[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