Panasonic Youth rob sanheim writes about software, business, ruby, music, stuff and things



Posted
5 September 2006 @ 10pm

Tagged
Rails, Ruby, TDD

Discuss

Accessing Rails Configuration and database.yml Programatically

I changed our database.yml to use merge keys, but we have a case where the host is different for production from all the other environments. I wanted to test and ensure the prod db host remained correct with the yaml merge - the docs say it works like hash#merge, but you don't wanna mess around with your production environment.

So here's how to access your configuration programmatically, so you can test it - note that "database_configuration" is a hash with the values from database.yml.

RUBY:
  1. def test_db_host_configuration
  2.   config = Rails::Configuration.new
  3.   assert_equal "prod_host", config.database_configuration["production"]["host"]
  4.   assert_equal "prod_host", config.database_configuration["other_production"]["host"]
  5.   assert_equal "localhost", config.database_configuration["development"]["host"]
  6.   ... # etc
  7. end

Take at a look at initalizer.rb in the rails source to see what is all on the configuration object, you can lots of useful stuff there. Note that this is creating a new config object, not reading the in-memory one, so I suppose if you do crazy stuff like modify config values on the fly this wouldn't be reliable.


1 Comment

Posted by
GregoryBluvshteyn
19 April 2007 @ 11am

So do you know the way of how to read in memory config, and if there a way to do that at all ?


Leave a Comment

Deprecation in Rails, and What You Can Do About It Seeking Alpha now carried by Yahoo! Finance