# Simple SQL query from a legacy db

**URL:** <https://www.ruby-forum.com/t/simple-sql-query-from-a-legacy-db/92493>\
**Category:** Rails\
**Created:** [April 20, 2007, 12:47am UTC](https://www.ruby-forum.com/t/simple-sql-query-from-a-legacy-db/92493 "2007-04-20T00:47:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gabez](https://www.ruby-forum.com/u/gabez)\
**Post date:** [April 20, 2007, 12:47am UTC](https://www.ruby-forum.com/t/simple-sql-query-from-a-legacy-db/92493/1 "2007-04-20T00:47:50Z")

</div>

Hi,

Im pretty new to Ruby as I’ve been programming for it for less than a  
month. I’m a php programmer that switched 🙂

Anyways, I’m having an issue. I have my full site built in php and I’m  
using a legacy mysql database.

I am building additions to my site with ruby on rails. However, my  
problem is that I need to be able to look at my live legacy database and  
grab a username and password from it. So all I need are two grab two  
variables from in there for user authentication, thats it.

This has proved to be quite the challenge! The only solution I found is  
using something called DBI which I have to download and install on top  
of Ruby. This seems like overkill for such a simple query!

Is there any way to do this simply with Ruby? It just seems ridiculous  
that there wouldnt be a way to do such a basic thing…

---

<div class="post-metadata">

**Author:** ![gabez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gabez](https://www.ruby-forum.com/u/gabez)\
**Post date:** [April 20, 2007, 1:15am UTC](https://www.ruby-forum.com/t/simple-sql-query-from-a-legacy-db/92493/2 "2007-04-20T01:15:17Z")

</div>

Where there is a will there is a way.  
There is a common misconception that Rails cannot connect to two  
databases, and this is not entirly true. You can connect rails to  
multiple databases(you can’t connect models to multiple database, well  
at least not easily). So in this case, you’d connect rails to your  
main DB(which I presume you already have) and then also connect it to  
the legacy database but you just have a User model that uses that  
second db connection. So anytime you do a User.find(:all) or maybe in  
your case User.find\_by\_sql(insert sql statement here) you will be  
interacting with your old db.

Does that make any sense at all?

Cheers,  
Cam

---

<div class="post-metadata">

**Author:** ![gabez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gabez](https://www.ruby-forum.com/u/gabez)\
**Post date:** [April 20, 2007, 1:17am UTC](https://www.ruby-forum.com/t/simple-sql-query-from-a-legacy-db/92493/3 "2007-04-20T01:17:17Z")

</div>

On Apr 19, 2007, at 6:47 PM, Gab Gabi wrote:

> variables from in there for user authentication, thats it.  
> You can override a lot of the defaults from ActiveRecord in order to  
> work with legacy databases, so that you can perform something like  
> the above with ActiveRecord.

class MyTable \< ActiveRecord::Base  
set\_primary\_key “my\_key\_name”  
set\_table\_name “my\_table\_name”  
end

See [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowToUseLegacySchemas) for  
more examples.

If your rails app and this model use entirely different databases.  
See the comments on this post for more on that (there may be a better  
reference but this is the best 2 minutes with google pulled up):

[http://blog.amber.org/2005/06/30/lost-in-multiple-rails/](http://blog.amber.org/2005/06/30/lost-in-multiple-rails/)

James.

–  
James S.  
Play: [http://james.anthropiccollective.org](http://james.anthropiccollective.org)  
Work: [http://jystewart.net/process/](http://jystewart.net/process/)

---

<div class="post-metadata">

**Author:** ![gabez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gabez](https://www.ruby-forum.com/u/gabez)\
**Post date:** [April 21, 2007, 5:49am UTC](https://www.ruby-forum.com/t/simple-sql-query-from-a-legacy-db/92493/4 "2007-04-21T05:49:33Z")

</div>

Perfect!  
Worked!

Thanks James 😃

---

<div class="post-metadata">

**Author:** ![gabez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gabez](https://www.ruby-forum.com/u/gabez)\
**Post date:** [June 11, 2007, 5:04am UTC](https://www.ruby-forum.com/t/simple-sql-query-from-a-legacy-db/92493/5 "2007-06-11T05:04:44Z")

</div>

On Apr 19, 4:15 pm, James S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
[…]

> more examples.  
> […]

Thank you for the link 🙂

-Thufir
