Skip to Content

Rails config for

Posted on

From version 4.2, Rails provides a beautiful feature to load config files.

# config/redis.yml
development:
  host: localhost
  port: 6379
test:
  host: localhost
  port: 6379
production:
  host: redis-production
  port: 6379
irb(main):001:0> Rails.application.config_for(:redis)
=> {"host"=>"localhost", "port"=>6379}

Now, we can call config_for(:redis), Rails will look for config/redis.yml, parse it and returns right configuration for your RAILS_ENV.

I think it’s a great way to store configuration for 3rd party apps (elasticsearch, redis, apple, google, ….)

Moreover, we can add this line to application.rb

# application.rb
config.redis = config_for(:redis)

and now we can call it in rails console.

Rails.configuration.redis

References

comments powered by Disqus