title: Strategies for overriding database.yml?
body: <p>In my environment, the deployment servers have much of the connection information that is in the database.yml. That is, they know whether they are development, test or production servers, and they know their respective database connection information.</p>
  
  <p>I can encapsulate this information in a Server class for example, so that I can retrieve the information:</p>
  
  <pre><code>Server["environment"] #=&gt; production
  Server["db_host"] #=&gt; db5.example.com
  Server["db_password"] #=&gt; [a decrypted password]
  </code></pre>
  
  <p>and so on. I would like to deploy a Rails application and have it autoconfigure based on the Server settings. What is the best way to do this?</p>
  
  <p>One way to do this is Erb in my database.yml:</p>
  
  <pre><code>&lt;%= Server["environment"] %&gt;: 
    adapter: oracle_enhanced
    host: &lt;%= Server["db_host"] %&gt;
    username: db_user 
    password: &lt;%= Server["password"] %&gt;
  </code></pre>
  
  <p>I'm not too thrilled about doing it this way, but it would work. In this case, where would I put the 'server.rb' that defines the Server class--require it here in the yml? app/initializers gets loaded after ActiveRecord loads database.yml. </p>
  
  <p>Another possible solution is to somehow override railties' database initializer:</p>
  
  <pre><code># File railties/lib/initializer.rb, line 903
  def database_configuration
    require 'erb'
    YAML::load(ERB.new(IO.read(database_configuration_file)).result)
  end
  </code></pre>
  
  <p>The above is called only if :active_record is defined in config.frameworks. I'm not sure how I would go about overriding this early enough in the Rails startup sequence.</p>
  
  <p>Maybe a third option is to <em>remove</em> :active_record from config.frameworks, and then create the connection later, say in the app initializers? I'm afraid this may have lots of unintended side effects.</p>
  
  <p>I'm <em>hoping</em> that there's something simple and obvious that I haven't found, such as an ActiveRecord feature that allows me to opt out of database.yml and provide alternate config programmatically.</p>