Ruby rails: configuration

From wikinotes

Documentation

official docs https://guides.rubyonrails.org/configuring.html

Locations

{project}/config/application.rb config for entire rails app
{project}/config/environments/production.rb component configuration
{project}/config/database.yml database config
{project}/config/**/{anything}.yml arbitrary system configs (see Custom below)

Application

Basics

Rails divides it's configuration in environment-specific files, and one universal configuration.
The format of these files is identical.

  • config/application.rb (global)
  • config/environments/(development|test|production).rb


This file exposes/overrides configuration for the various gems that together comprise rails.
Gems are configured under their own config.{gem}.* namespaces.
You can add your own custom configuration here under config.x.*.

Examples

# gem configuration
config.active_record.schema_format = :sql
config.autoload_paths = ['lib', 'something/else']

# custom configuration
config.x.my_key = "foo"

You can access these within the application under the Rails namespace.

Rails.configuration.x.my_key # 'foo'

Common Options

# rails will auto-require modules relative to these paths
# (which in turn are relative to projectroot)
config.autoload_paths = ['lib', 'something/else']


Database

Environment

# override database settings
DATABASE_URL=postgresql://localhost/my_database

config/database.yml

test database configuration https://guides.rubyonrails.org/testing.html#the-test-database

db/seeds.rb

Create core database data here, then use rake to populate database.

# db/seeds.rb
City.create(:name => 'Chicago')
City.create(:name => 'Copenhagen')
Currency.create(:name => 'CAD')
Currency.create(:name => 'USD')
rake db:seed   # load seeds.rb
rake db:setup  # create database, load seeds.rb
rake db:reset  # drop database, create database, load seeds.rb

Custom

You can add arbitrary config files under ${project}/config/*.yml that define environment specific configuration options.

# $PROJECT/config/foo.yml
development:
  bar: 1

production:
  bar: 2

test:
  bar: 3
Rails.application.config_for(:foo)
# {bar: 1}