# Changing a model's database connection

**URL:** <https://www.ruby-forum.com/t/changing-a-models-database-connection/191849>\
**Category:** Rails\
**Created:** [July 19, 2010, 8:12pm UTC](https://www.ruby-forum.com/t/changing-a-models-database-connection/191849 "2010-07-19T20:12:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de0a6/32.png) [@MichaSSSSl](https://www.ruby-forum.com/u/MichaSSSSl)\
**Post date:** [July 19, 2010, 8:12pm UTC](https://www.ruby-forum.com/t/changing-a-models-database-connection/191849/1 "2010-07-19T20:12:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MichaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de0a6/32.png) [@MichaSSSSl](https://www.ruby-forum.com/u/MichaSSSSl)\
**Post date:** [July 19, 2010, 8:14pm UTC](https://www.ruby-forum.com/t/changing-a-models-database-connection/191849/2 "2010-07-19T20:14:55Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![MichaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de0a6/32.png) [@MichaSSSSl](https://www.ruby-forum.com/u/MichaSSSSl)\
**Post date:** [July 19, 2010, 8:37pm UTC](https://www.ruby-forum.com/t/changing-a-models-database-connection/191849/3 "2010-07-19T20:37:31Z")

</div>

On Jul 19, 6:08 pm, Michaël [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![MichaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de0a6/32.png) [@MichaSSSSl](https://www.ruby-forum.com/u/MichaSSSSl)\
**Post date:** [July 19, 2010, 8:45pm UTC](https://www.ruby-forum.com/t/changing-a-models-database-connection/191849/4 "2010-07-19T20:45:46Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![MichaSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de0a6/32.png) [@MichaSSSSl](https://www.ruby-forum.com/u/MichaSSSSl)\
**Post date:** [July 19, 2010, 8:43pm UTC](https://www.ruby-forum.com/t/changing-a-models-database-connection/191849/5 "2010-07-19T20:43:23Z")

</div>

On 19 July, 19:32, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.
