Ruby on Rails lessons applied to PHP
Ruby on Rails is a programming framework for building websites using the Ruby language. Everything is set up to work a certain way; for example, the MVC (Model-View-Controller) model. What this means is that database stuff is put in the model area, the controller contains application logic, and the view contains mostly HTML. It’s set up this way to separate code into logical areas to make it more maintainable.
Ruby on Rails is also very specific with how it wants its databases structured. The primary key must be labeled “id”, the foreign keys are a combination of the table and “_id” (with rules dealing with pluralization of keys, relationships, and tables).
This is so that their “Active Record” framework can work properly. Active record is a system that makes creating dynamic database queries really easy.
All of this is good, except when trying to attach to databases and systems that aren’t “rails-friendly”. Legacy systems with database schemas that aren’t compatible with the way Rails wants the system to be written can be a chore to get to work in Rails. It can be done, but the magic disappears rapidly.
My photography site, The Lens Flare, is one such example. The site is a combination of custom PHP code, an old PHP forum, and a PHP-based Wiki. Everything is tied together using a library of custom functions written in PHP, and it works well.
I started to rewrite the site using Ruby on Rails, but to do so, I’d have to make drastic changes to the database, and I had problems trying to blend the Forum and Wiki into the Rails code since they would remain PHP, and Rails would need to access those databases. I’m sure it could be done, but it wasn’t worth the headache.
Since then, I’ve rewritten the photography site in PHP and am nearing the completion of the project. After learning how Rails does it, I’ve applied their logic in many different places. The code is split up much more modular in this version. In as many places as possible, application logic and database queries are run separate and passed into the HTML code as variables. I’m using a combination of functions and include files like I’d use Partials in Rails.
All in all, I feel that learning Ruby on Rails has improved my PHP programming skills. I now program completely new projects in RoR, and maintain existing projects in the same language they were written in unless the entire project will be rewritten.
Since PHP is more flexible, it’s easier to use PHP to tap into a Rails-friendly database than it is to use Rails to tap into a database created for a PHP project.
