views:

49

answers:

1

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

+1  A: 

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.

Jakub Hampl
Thanks for the input. It's a rather small db, and the structure isn't completely horrible, just a little bit unnormalised. However I'd prefer to take the approach of using a customised model. Any examples/resources you could point me toward? Thanks
the_snitch
Pretty much the ActiveRecord docs should lead you forward. Remember you can always use virtual attributes and if there is a strait-forward mapping between your rails names and your db names you can always write a `method_missing`.
Jakub Hampl