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,

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

 def build\_report:

    Customer\.establish\_connection\(
        :adapter  =&gt; &quot;mysql&quot;,
        :host     =&gt; &quot;hostname&quot;,
        :username =&gt; &quot;user&quot;,
        :password =&gt; &quot;password&quot;,
        :database =&gt; &quot;schema&quot;
    \)

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

    \.\.\.\.\. &lt;report logic here&gt;

 end

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

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

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.

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,