开发者

Connect Rails model to non-rails database

开发者 https://www.devze.com 2022-12-30 14:37 出处:网络
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'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 开发者_如何学Cto 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


This really depends on how esoteric your legacy db is. This affects the solution considerably. If your legacy db is quite similar to Rails conventions then using a model with a few customizations will probably prove as the best approach. However I've heard of people who wrote a script that constantly reimported data from the legacy db into a new db - the whole structure of the db was so wrong that approach was worth it.

0

精彩评论

暂无评论...
验证码 换一张
取 消