Skip to content

Instantly share code, notes, and snippets.

@murayama
Created July 9, 2013 01:41
Show Gist options
  • Save murayama/5954004 to your computer and use it in GitHub Desktop.
Save murayama/5954004 to your computer and use it in GitHub Desktop.
rails起動時にモデルに対して任意の設定をする

モデルに以下のように、クラス変数を用意し、mattr_accessorでアクセスできるようにしておく
ポイントは、self.setupメソッドのyield self

# app/models/user.rb
class User < ActiveRecord::Base

  mattr_accessor :hoge_setting
  # デフォルト値を設定しておく
  @@hoge_setting = true

  def self.setup
    yield self
  end

end

config/initializerにuser.rbを作成し、以下のようにする

# config/initializers/user.rb
User.setup do |config|

  config.hoge_setting = false

end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment