Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 274
Nona Avatar asked Dec 08 '22 07:12

Nona


1 Answers

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
like image 199
Patrick Oscity Avatar answered Feb 19 '23 18:02

Patrick Oscity