New to RoR. New project with a legacy database and php service. Any tips?

Hi All,

I'm about to jump into Ruby on Rails for a new project, but wanted to see if anyone has some tips for my specific situation:

Basically I already have a web service in PHP that communicates with a non-web based application to sync data between instances, storing the data in MySQL. This is all working great, and there is no need to change it.

I now wish to add a web based front end to this system, and am looking at RoR to do it.

So there's not much legacy code (since there is no web site right now), but there is a legacy database, and a few methods that I may need to expose to Ruby from PHP.

Regarding the legacy database: - Any pointers on reconfiguring an existing database to use rails? Do I *have* to rename all the primary keys to 'id' ? - Should I re-create the structure in migrations, or just manage things the old way (i.e. big list of sql update statements)

The web site will be basic CRUD, but with one exception:

I can't just use the standard "Update" functions of RoR (i.e. the ones that map to an UPDATE SQL statement). The design is this: I never update any rows on this database, rather I insert the record again, and mark the old one as "old". This is done to give users an unlimited undo function (think a little like how Wordpress does it)

- Do you have any suggestions to get me started on overriding the model's standard update function to instead do this special handling?

- I may also need to override the "Read" function to use not the primary key, but different unique key I give the record, one that doesn't change between revisions (and also to only return the newest version). Will this be difficult?

Finally, what is the simplest way to interface with an internal PHP service? Do a JSON formatted http://localhost request and just get back the array of data?

Thanks in advance for your help.

Regards, Will

William Denniss wrote in post #955352:

Hi All,

I'm about to jump into Ruby on Rails for a new project, but wanted to see if anyone has some tips for my specific situation:

Basically I already have a web service in PHP that communicates with a non-web based application to sync data between instances, storing the data in MySQL. This is all working great, and there is no need to change it.

I now wish to add a web based front end to this system, and am looking at RoR to do it.

I assume you're looking to have Rails work with that MySQL database? If so, then any concerns about php and the sync'ing service are moot.

Regarding the legacy database: -Any pointers on reconfiguring an existing database to use rails? Do I *have* to rename all the primary keys to 'id' ? -Should I re-create the structure in migrations, or just manage things the old way (i.e. big list of sql update statements)

You don't *have to* add an id column to all your tables, but it will definitely make life easier for creating your Rails app if you do, otherwise just be sure to read up on your ActiveRecord methods. Using something other than id as the key is definitely supported.

The web site will be basic CRUD, but with one exception:

I can't just use the standard "Update" functions of RoR (i.e. the ones that map to an UPDATE SQL statement). The design is this: I never update any rows on this database, rather I insert the record again, and mark the old one as "old". This is done to give users an unlimited undo function (think a little like how Wordpress does it)

- Do you have any suggestions to get me started on overriding the model's standard update function to instead do this special handling?

Might be a couple of ways to do this, but it kind of depends on how you mark a record as 'old' - in the past I've used a 'valid_until' field. Any current, valid records just don't have an expiration date, expiring a record could be done by setting that value just before creating the new record (before_save callback?).

- I may also need to override the "Read" function to use not the primary key, but different unique key I give the record, one that doesn't change between revisions (and also to only return the newest version). Will this be difficult?

Not hard, see ActiveRecord mentioned above.

Finally, what is the simplest way to interface with an internal PHP service? Do a JSON formatted http://localhost request and just get back the array of data?

Your Rails app will have to post requests in a format understood by the php service, and deal with whatever it gets back, be that Json or POX or something else.

William Denniss wrote in post #955352:

Hi All,

I'm about to jump into Ruby on Rails for a new project, but wanted to see if anyone has some tips for my specific situation:

Have you ever used Rails before? If not, then I advise you not to do this as your very first Rails project. Learn about how Rails works by default before you try to use it in a situation like this.

Basically I already have a web service in PHP that communicates with a non-web based application to sync data between instances, storing the data in MySQL. This is all working great, and there is no need to change it.

Your phrasing, if I am understanding it correctly, betrays an assumption that may or may not be valid. "There is no need to change it" may be true of the system as it currently stands, but you are adding a new component to the system. Thus, there may soon be a need to change the existing parts -- or there may not. Don't lock yourself into one or the other view a priori.

I now wish to add a web based front end to this system, and am looking at RoR to do it.

So there's not much legacy code (since there is no web site right now), but there is a legacy database, and a few methods that I may need to expose to Ruby from PHP.

Will the other application still be updating this DB, or will the Rails app now be the only thing touching it?

Regarding the legacy database: -Any pointers on reconfiguring an existing database to use rails?

What does it look like now?

Do I *have* to rename all the primary keys to 'id' ?

No. You can override most of Rails' defaults if you really want to. Over time, though, you might want to get more Railsy.

-Should I re-create the structure in migrations, or just manage things the old way (i.e. big list of sql update statements)

Neither! Use rake db:schema:dump to get the current structure into your db/schema.rb file (and of course, put that in version control); then do all *further* DB changes with migrations.

The web site will be basic CRUD, but with one exception:

I can't just use the standard "Update" functions of RoR (i.e. the ones that map to an UPDATE SQL statement).

Yes you can. Read on. Again, you are ruling things out a priori that may in fact be useful.

The design is this: I never update any rows on this database, rather I insert the record again, and mark the old one as "old". This is done to give users an unlimited undo function (think a little like how Wordpress does it)

- Do you have any suggestions to get me started on overriding the model's standard update function to instead do this special handling?

- I may also need to override the "Read" function to use not the primary key, but different unique key I give the record, one that doesn't change between revisions (and also to only return the newest version). Will this be difficult?

There are several Rails plugins that already do this (acts_as_versioned, vestal_versions, etc.). Check them out.

Finally, what is the simplest way to interface with an internal PHP service? Do a JSON formatted http://localhost request and just get back the array of data?

ActiveResource, probably.

Thanks in advance for your help.

Regards, Will

Best,