4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails enum_help keyとvalueを入れ替える

Posted at

enum_helpは便利なgem。
おしりに_i18nをつけるだけで日本語を出してくれる。

Human.star_signs_i18n
# => {"aries"=>"牡羊座",
 "taursu"=>"牡牛座",
 "gemini"=>"双子座",
 "cancer"=>"蟹座",
 "leo"=>"獅子座",
 "virgo"=>"乙女座",
 "libra"=>"天秤座",
 "scorpio"=>"蠍座",
 "sagittarius"=>"射手座",
 "capricorn"=>"山羊座",
 "aquarius"=>"水瓶座",
 "pisces"=>"魚座"}

キーとバリューを入れ替えたい時はinvertメソッド。

Human.star_signs_i18n.invert
# => {"牡羊座"=>"aries",
 "牡牛座"=>"taursu",
 "双子座"=>"gemini",
 "蟹座"=>"cancer",
 "獅子座"=>"leo",
 "乙女座"=>"virgo",
 "天秤座"=>"libra",
 "蠍座"=>"scorpio",
 "射手座"=>"sagittarius",
 "山羊座"=>"capricorn",
 "水瓶座"=>"aquarius",
 "魚座"=>"pisces"}

いつ使うの?

= f.select :star_sign, Human.star_signs_i18n.invert

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?