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

狂风中的少年 提交于 2019-12-10 15:25:36

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/24321632/where-does-database-yml-get-loaded-in-activerecord-in-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!