1
0

More than 1 year has passed since last update.

【Ruby/Rails】定数の定義と管理方法

Posted at

config gemを使う

config/settings/development.ymlなどファイルを作成して環境ごとに管理する。

下記のように呼び出して使える。

Settings.SERVICE_NAME
#=> "hoge_app"

Settings[:SERVICE_NAME]
#=> "hoge_app"

クラス内で定義

文脈としてあるクラスの中だけで使われるような定数はそのクラスに定義する。
freezeを忘れずに!

model/post.rb
class Post < ApplicationRecord
...
  MAX_COMMENT_COUNT = 100.freeze
...
end

参考

1
0
1

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