Help us understand the problem. What is going on with this article?

Rails enum_help keyとvalueを入れ替える

More than 5 years have passed since last update.

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

rinkun
30を目前に「手に職」への憧れが捨てられず、サラリーマンをやめて、フリーランス、プログラミングの世界に飛び込みました。 WEBをメインに、エクセルVBAとキッズプログラミングでScratchとUnityを少々。 最近はPWAとPhina.jsに興味があります。 サークルアラウンド(株)佐藤正志氏に師事。
https://papagram.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away