【问题标题】:where does database.yml get loaded in ActiveRecord in Rails 4?在 Rails 4 的 ActiveRecord 中,database.yml 在哪里加载?
【发布时间】:2014-06-20 06:57:36
【问题描述】:

Rails 应用程序的 config/database.yml 在 ActiveRecord 代码库中的哪一行(或方法)被加载? (我特别关注 4.0.5,但如果有人有任何关于 >=4.0.5 的信息,那将很有启发性)?

【问题讨论】:

标签: ruby activerecord ruby-on-rails-4 rails-activerecord


【解决方案1】:

它在 Railties 内部,特别是在文件 railties/lib/rails/application/configuration.rb 的第 101–116 行(对于 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】:

    在 Rails 4.0.6 中,“database.yml”文件的位置设置在 railties configuration 内:

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

    然后加载此文件(如上一个答案所建议的那样)a few lines below:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 2014-02-23
      • 2015-03-28
      相关资源
      最近更新 更多