3
0

More than 1 year has passed since last update.

Model.model_name.humanなどについて

Posted at

いつ使うのか

Model.model_name.humanはいつ使われるのかというとI18nと呼ばれる国際化をする際に使われます。
実際にこのように書かれています。
config/locals/ja.yml

ja:
  activerecord:
    errors:
      models:
        user:
          attributes:
            name:
              blank: "が空になっています。入力してください。"
            email:
              blank: "が空になっています。入力してください。"
    models: 
      user: "会員"
    attributes:
      user:
        name: "名前"
        email: "Eメール"

このようにすることで、エラーメッセージ等の文言を日本語化することができるんです。
models階層の文字列を取得するには、

User.model_name.human
=> 会員

I18n.t("activerecord.models.user")
=> 会員

attribute階層の文字列を取得するには

User.human_attribute_name :email
=> Eメール

I18n.t("activerecord.attributes.user.email")
=> Eメール

書き方が似ているようで微妙に違うので、わからなくなったら当記事を参照してください
以上です。何か間違いがございましたら、ご教示いただけますと幸いです。

【参考資料】

3
0
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
3
0