On 4/19/06, Philip Hallstrom <rails@philip.pjkh.com>
wrote:> I''m wondering if there''s a way to split up the reads and
writes in Rails
> so that pages/actions that don''t need to write can query from a
group of
> mirrored databases. And my content managers can update a single master.
Just making this up as I go along, this is ugly and may not work at
all. Inspiration
came from:
http://wiki.rubyonrails.com/rails/pages/HowtoUseMultipleDatabases
http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc
Model:
class Model < ActiveRecord::Base
...
end
class ReadModel1 < Model
set_table_name "models"
establish_connection ...
end
class ReadModel2 < Model
set_table_name "models"
establish_connection ...
end
...
class ReadModelN < Model
set_table_name "models"
establish_connection ...
end
class WriteModel < Model
set_table_name "models"
establish_connection ...
end
Managers Controller:
def list
@models = WriteModel.find :all
end
Users Controller:
@@connections = [ReadModel1, ReadModel2, ... ReadModelN]
def list
read_model = @@connections[rand N]
@models = read_model.find :all
end
Assuming this works at all :-), an enhancement would be to store which model
class to use in the session once and reuse the same connection for the rest
of that session.