Hello,
I need to access an mirrored database with one read and one write
cluster. Is there a way to tell a model to use two connections? So that
all updates etc go to the write database and all read from the read one?
This is a fixed set up I can not use MySQL 5.x stuff and additionally i
get a token on write, which can be checked on read (and wait for
replication) if new data is needed.
I saw an open ticket on http://dev.rubyonrails.org/ticket/2041 but it
seems to be dead.
For me it would be enough to have the possibility to change the
connection of a model on runtime (e.g. block)
Model.use_read do
do some stuff
x= Model.find()
x.test
end
or
token = Model.use_write do
x = Model.new
x.save
end
Any idea how this can be achieved?
Is it possible to use
ActiveRecord::Base.establish_connection(:adapter => “mysql” ,
:host => “localhost” , :database => “railsdb” )
more dynamically?
Maybe creating a inherited model would do it?:
class Order < ActiveRecord::Base
end
ActiveRecord::Base.establish_connection(:adapter => “mysql” ,
:host => “localhost” , :database => “railsdb_read” )
class ReadOrder < Order
end
ActiveRecord::Base.establish_connection(:adapter => “mysql” ,
:host => “localhost” , :database => “railsdb_write” )
class WriteOrder < Order
end
Any other clues?