1
0

RailsのModelに定数を用意する

Posted at

概要

  • RailsのModelで定数を用意したかった
  • そのメモ

結論

こうすればよい

class MyModel < ApplicationRecord
  MY_CONSTANT = '定数の値'.freeze
end

freezeについて

調べてる感じ、やった方が良さそう。下記ができちゃうので

class MyModel < ApplicationRecord
  MY_CONSTANT = '定数の値'
end

MyModel::MY_CONSTANT << '追加の値'  # これができてしまう
puts MyModel::MY_CONSTANT  # => '定数の値追加の値'
1
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
1
0