Forum: Ruby on Rails Simple SQL query from a legacy db

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
727f1642b23c6d962a69b4848ec5d2a4?d=identicon&s=25 Gab Gabi (gabez)
on 2007-04-20 00:47

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..
B01fdd12039d82427a4af02360ef5571?d=identicon&s=25 cammo (Guest)
on 2007-04-20 01:15
(Received via mailing list)
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?

4715ae8a9ac5152600ccd3012267ab6b?d=identicon&s=25 James Stewart (Guest)
on 2007-04-20 01:17
(Received via mailing list)
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 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 Stewart
727f1642b23c6d962a69b4848ec5d2a4?d=identicon&s=25 Gab Gabi (gabez)
on 2007-04-21 05:49

Thanks James :D
E4e17e45f2e8cc967a8695131910f365?d=identicon&s=25 Thufir (Guest)
on 2007-06-11 05:04
(Received via mailing list)
On Apr 19, 4:15 pm, James Stewart <> wrote:
> more examples.

Thank you for the link :)

This topic is locked and can not be replied to.