# Creating a MysqlAdapter connection

**URL:** <https://www.ruby-forum.com/t/creating-a-mysqladapter-connection/129816>\
**Category:** Rails\
**Created:** [February 21, 2008, 7:47pm UTC](https://www.ruby-forum.com/t/creating-a-mysqladapter-connection/129816 "2008-02-21T19:47:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Wood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@Jon\_Wood](https://www.ruby-forum.com/u/Jon_Wood)\
**Post date:** [February 21, 2008, 7:47pm UTC](https://www.ruby-forum.com/t/creating-a-mysqladapter-connection/129816/1 "2008-02-21T19:47:32Z")

</div>

Hi,

I’m trying to connect to a Mysql database using MysqlAdapter, so I can  
migrate some data from a system that is being replaced by a Rails  
application. This requires having an open connection to two different  
databases, one of which I will query directly, and the other being  
used for the new application’s models.

I started with ActiveRecord::Base.establish\_connection to connect to  
the previous apps database, but when I do that it changes the models  
in my new application to attempt to use that database as well, making  
it impossible to save the newly migrated data.

Can somebody give an example of how to create a standalone connection,  
which isn’t being tracked by ActiveRecord? I’ve tried  
MysqlAdapter.new, but I have no idea what the parameters are meant to  
be (despite several hours of running in circles through ActiveRecord’s  
internals), so I havn’t been able to open a connection yet.

Thanks,

Jon

---

<div class="post-metadata">

**Author:** ![Jon\_Wood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@Jon\_Wood](https://www.ruby-forum.com/u/Jon_Wood)\
**Post date:** [February 21, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/creating-a-mysqladapter-connection/129816/2 "2008-02-21T20:17:39Z")

</div>

Just in case anyone comes across this, I ended up solving it by  
establishing a new connection for each ActiveRecord model as it’s  
loaded - it’s a bit of a hack, but it’s got my code running, so I’m  
happy for now.

class ActiveRecord::Base  
def self.inherited(subclass)  
super(subclass)  
subclass.establish\_connection(RAILS\_ENV)  
end  
end

I’m not sure that I need the call to super there, but I’m leaving it  
in just for safety.

Jon

---

<div class="post-metadata">

**Author:** ![Jon\_Wood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@Jon\_Wood](https://www.ruby-forum.com/u/Jon_Wood)\
**Post date:** [February 21, 2008, 9:51pm UTC](https://www.ruby-forum.com/t/creating-a-mysqladapter-connection/129816/3 "2008-02-21T21:51:21Z")

</div>

What I did to migrate data from MS Access to SQL Server. I just created  
a raw connection to Access and used DBI/DBD calls to pull back data.  
There wasn’t much point in even trying to use ActiveRecord because the  
Access DB didn’t conform to any model objects.

To put data into SQL Server, I used my ActiveRecord models. So, pulling  
the data from Access, I would use it to create new models that I would  
save. If you’re thinking “Gee, that’s inefficient,” wrap the creation  
of model objects in transactions. Also, unless you’re migrating data on  
a regular basis, you should only need to run this migration in the  
process of developing your application (I did the data migration code  
inside a Rails migration).

Another advantage of using the combination of raw DBI/DBD calls from the  
source database and Models for the destination database is the case  
where 1 row in the source table maps to multiple objects in the  
destination database.

paul
