Changing a model''s database connection

Michaël
Mon, 19 Jul 2010 17:08:36 -0000

Hi,

I''ve got a simple problem. I''ve got a collection of models,
Customer,
Product and Shop, and they''re all using the database configured for
the ActiveRecord class. However, for one particular task -- generating
a report -- I want to be able to set those models to use a database on
a different server. The secondary database is simply a copy of the
first, so the schemas and data is identical to the main database, but
I want to use this secondary server so that the load of producing
reports doesn''t affect the main database server.

I''ve tried the following, but it doesn''t seem to have any
effect:

     def build_report:

        Customer.establish_connection(
            :adapter  => "mysql",
            :host     => "hostname",
            :username => "user",
            :password => "password",
            :database => "schema"
        )

        @customer = Customer.find(@criteria[:customer_id])

        ..... <report logic here>

     end

Is it possible to achieve what I want to do this way, or do I need to
look along other lines?

Many thanks,
Michaël