More Advanced Database Features and Rails - Magic Multi-Connections (Page 3 of 4 )
Dr Nic Williams's Magic Multi-Connections gem (http://magicmodels.rubyforge.org/ magic_multi_connections/) allows you to connect to different databases concurrently from the same application. This is very useful when using one master and several read-only slaves serving the same models. The syntax is transparent; it uses module namespaces and imports the models (ActiveRecord::Base subclasses) into the namespaces.
For a single-master situation, you could define another database connection in database.yml for the read slave:
This database is backed by a module, which mirrors the ActiveRecord classes using this database connection:
require 'magic_multi_connections' module ReadSlave establish_connection :read_slave end
Now, all pre-existing models can be accessed through the read_slave connection by prefixing the model class with ReadSlave::.
# use the read-only connection @user = ReadSlave::User.find(params[:id])
# write to the master (can't use @user.update_attributes because it would # try to write to the read slave) User.update(@user.id, :login => "new_login")