Changing a model's database connection

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

Michaël wrote:

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 think you would be better advised to use your DB server’s
clustering/load-balancing features for this. It should not be your
application’s concern.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Jul 19, 6:08 pm, Michaël [email protected] wrote:

        :database => "schema"

this would only affect queries done against the customers table.
queries to any other tables would still go to the ‘normal’ database.
Is that what’s happening?

Fred

Michaël wrote:
[…]

As far as load balancing is concerned, my ultimate goal is to push
that to the database level and have the database server handle that,
but I need to implement something quickly as an intermediate step.

Then do it on the DB side now. It probably won’t take any more time
than all the research you’re already doing to do this on the app side,
and you won’t have to throw the work away later.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On 19 July, 19:32, Frederick C. [email protected]
wrote:

    ..... <report logic here>

Fred
That’s actually what I want to achieve, but I’ve done the above and it
doesn’t seem to work. I know that reports only use information from
the Customers, Products and Shop models, so I only want to change the
database connection for these three while generating the report, and
then switch it back after its done.

As far as load balancing is concerned, my ultimate goal is to push
that to the database level and have the database server handle that,
but I need to implement something quickly as an intermediate step.