[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 ( ■ 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


 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.


<%= 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