The code to go with the Tutorial published over on my website.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Client Database Switching

How to automatically switch client databases using Rails.

This is the code that accompanies my Tutorial on the matter and is based on the orignal solution Mark Somerville wrote at PCCL in 2007.


  1. Install as a plugin
  2. Create Directory /config/databases in your Rails app root
  3. Create /config/databases/localhost.yml in your Rails app root
  4. With contents:
	<%= { "database_details" => ActiveRecord::Base.configurations["development"] }.to_yaml %>

Then, add in your Application Controller:

	prepend_before_filter :choose_database_from_host

YAML File Format

Databases config files must take the following form:

	# Contents of
	  adapter: mysql
	  database: client_db_name
	  username: root