Connect Rails model to non-rails database

Posted by the_snitch on Stack Overflow See other posts from Stack Overflow or by the_snitch
Published on 2010-05-15T05:25:42Z Indexed on 2010/05/15 5:34 UTC
Read the original article Hit count: 242

Filed under:
|
|

I'm creating a new web application (Rails 3 beta), of which pieces of it will access data from a legacy mysql database that a current php application is using.

I do not wish to modify the legacy db schema, I just want to be able to read/write to it, as well as the rails application having it's own database using activerecord for the newer stuff. I'm using mysql for the rails app, so I have the adapter installed.

How is the best way to do this? For example, I want contacts to come from the old database. Should I create a contacts controller, and manually call sql to get the variables for the views? Or should I create a Contact model, and define attributes that match the fields in the database, and am I able to use it like Contact.mail_address to have it call "SELECT mailaddr FROM contacts WHERE id=Contact.id".

Sorry, I've never done much in Rails outside of the standard stuff that is documented well. I'm not sure of what the best approach would be. Ideally, I want the contacts to be presented to my rails application as native as possible, so that I can expose them RESTfully for API access.

Any suggestions and code examples would be much appreciated

© Stack Overflow or respective owner

Related posts about rails

Related posts about php