【Rails】定数は、config gemで管理しましょう


環境

$ rails -v
Rails 6.0.3.1
$ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin19]

まずは、Gemのインストール

Gemfile
gem 'config'
$ bundle install --path vendor/bundle

configの初期設定を行う

configの初期設定のために関連ファイルをインストールする

$ bundle exec rails g config:install

定数定義と、使い方

config/settings.yml
service: 
  name: 'vdeep' 
  url: 'http://vdeep.net'

authentication_password: "foobarbaz"
$ rails c

> Settings.service.name
=> "vdeep"

> Settings.service[:name]
=> "vdeep"

> Settings[:service][:name]
=> "vdeep"

> Settings.authentication_password
=> "foobarbaz"