Simple SQL query from a legacy db


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

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…

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?


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”

See Peak Obsession 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):


James S.


Thanks James :smiley:

On Apr 19, 4:15 pm, James S. [email protected] wrote:

more examples.

Thank you for the link :slight_smile: