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.
- Install as a plugin
- Create Directory /config/databases in your Rails app root
- Create /config/databases/localhost.yml in your Rails app root
- With contents:
<%= { "database_details" => ActiveRecord::Base.configurations["development"] }.to_yaml %>
Then, add in your Application Controller:
prepend_before_filter :choose_database_from_host
Databases config files must take the following form:
# Contents of ryanstenhouse.eu.yml database_details: adapter: mysql database: client_db_name username: root password: