# How to copy between databases?

**URL:** <https://www.ruby-forum.com/t/how-to-copy-between-databases/79249>\
**Category:** Rails\
**Created:** [March 9, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/how-to-copy-between-databases/79249 "2006-03-09T23:17:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [March 9, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/how-to-copy-between-databases/79249/1 "2006-03-09T23:17:22Z")

</div>

I want to copy data from one database to another using rails. But, it  
seems  
that rails only will talk to one database at a time. Is there a way  
around  
this?

Any help would be greatly appreciated.

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [March 9, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/how-to-copy-between-databases/79249/2 "2006-03-09T23:23:32Z")

</div>

> I want to copy data from one database to another using rails. But, it seems  
> that rails only will talk to one database at a time. Is there a way around  
> this?

You should be able to explicitly connect to extra databases. Take a  
look at:

```
http://wiki.rubyonrails.com/rails/pages/HowtoUseMultipleDatabases
http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc
```

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [March 10, 2006, 1:18am UTC](https://www.ruby-forum.com/t/how-to-copy-between-databases/79249/3 "2006-03-10T01:18:57Z")

</div>

Thanks for the links. The pragprog method of creating a LegacyBase  
parent  
model class, and having the other model classes inherit from it works.

My parent class is something like this:

class LegacyBase \< ActiveRecord::Base  
establish\_connection …  
end

Since this connection is used for importing data into my main  
database, should I worry about when it closes?

or, maybe a better way to say this is: How can I closed the  
connection when it is no longer needed?

On 3/9/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> http://wiki.rubyonrails.com/rails/pages/HowtoUseMultipleDatabases
> 
> ```
> 
> [http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc](http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [March 10, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/how-to-copy-between-databases/79249/4 "2006-03-10T17:24:21Z")

</div>

On 3/9/06, Larry K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Since this connection is used for importing data into my main database,  
> should I worry about when it closes?
> 
> or, maybe a better way to say this is: How can I closed the connection when  
> it is no longer needed?

[http://rubyonrails.org/api/classes/ActiveRecord/Base.html](http://rubyonrails.org/api/classes/ActiveRecord/Base.html)

```
remove_connection(klass=self)

    Remove the connection for this class. This will close the active
    connection and the defined connection (if they exist). The 

```

result  
can be used as argument for establish\_connection, for easy  
re-establishing of the connection.
