Nona
Nona

Reputation: 5462

where does database.yml get loaded in ActiveRecord in Rails 4?

What line (or method) in the ActiveRecord codebase does the config/database.yml for a Rails application get loaded? (I'm looking at 4.0.5 specifically, but if anyone has any information on >=4.0.5, that would be illuminating)?

Upvotes: 5

Views: 4752

Answers (2)

Patrick Oscity
Patrick Oscity

Reputation: 54684

It's inside the Railties, specifically in the file railties/lib/rails/application/configuration.rb in lines 101–116 (for Rails 4.0.5):

https://github.com/rails/rails/blob/v4.0.5/railties/lib/rails/application/configuration.rb#L101-L116

# Loads and returns the configuration of the database.
def database_configuration
  yaml = paths["config/database"].first
  if File.exist?(yaml)
    require "erb"
    YAML.load ERB.new(IO.read(yaml)).result
  elsif ENV['DATABASE_URL']
    nil
  else
    raise "Could not load database configuration. No such file - #{yaml}"
  end
rescue Psych::SyntaxError => e
  raise "YAML syntax error occurred while parsing #{paths["config/database"].first}. " \
        "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
        "Error: #{e.message}"
end

Upvotes: 14

gitcdn
gitcdn

Reputation: 485

In Rails 4.0.6, the location of the "database.yml"-File is set inside the railties configuration:

paths.add "config/database",    with: "config/database.yml"

This file then gets loaded (as the previous answer suggested) a few lines below:

YAML.load ERB.new(IO.read(yaml)).result

Upvotes: 3

Related Questions