My First Ruby on Rails project

It's been a while since I wanted to refactor my PHP+Smarty web site and also to implement additional database driven features. The main purpose of this application is to manage table tennis tournament results.


Being hosted on a PHP box, my first idea was to look for Ruby on Rails clone in PHP.
I found CakePHP to be the only serious one able to run on PHP4 so the choice was easy.
I spent few hours during 3 weeks to learn the framework and start my project, there was no book to help so I relied mostly on simple tutorials and users forums.
CakePHP is a good framework with may useful features (some of them are even more advanced than Ruby on Rails equivalent ones) and a great community but documentation is sparse and worse: it suffers from PHP's poor syntax (at least from a Java developer's standpoint).
After 3 weeks, I decided to stop and switched to Ruby on Rails even if it meant switching to a new web hosting company.

Switching to Ruby on Rails

I bought the "Agile Web Development with Rails" book and read several tutorials.

Using existing database

First, I created my application using rails ipttc then I created a migration from my existing database.

ruby script/generate migration Initial
rake db:schema:dump

Then I copy/pasted from schema.rb into the up method of db/migrate/001_initial.rb

rake db:migrate

I found that my legacy schema was not following some of Rails conventions like having a numerical id for tables, I decided to keep it simple and adapt the rails conventions, maybe I'll dig further if needed.

Being a lazy typist, I looked at ways to generate my models from existing database and I found Dr Nic's Magic Model Generator which created 7 models with validation rules in a snap, Thanks Doc! By reviewing the generated models, I was able to learn quickly about validations and relations and I really discovered something unexpected: it did create some kind of indirect relations: "has many through".

Tournament has many Medals.
Player has many Medals.
Tournament has many Players through Medals.

And it does make a lot of sense, it's like saying:

Tournament has many Medalists.

After that, I started to play with scaffolding and I was a bit disappointed because it did not take into account relations between models. I searched for better scaffolding and found ActiveScaffold and Hobo. ActiveScaffold provides dynamic scaffolding using Ajax and nice presentation. Hobo is more a framework on top of Rails and provide additional nice features like a security model with users, roles and permissions. Both of them are very promising but they provide only dynamic scaffolding while I am more looking for a code generator to be able to learn and modify things for my needs. Also, I'm still too new in Rails to be able to select an additional framework, I must try to understand the standard Rails features first.

Fortunately, I was also looking at RESTful implementation in Rails and found that there was a new scaffold code generator that was deprecating the original one.
The good thing about scaffold_resource is that it lets you to generate the model, the views, the controller and the database table in migration at once.
You just have to list the name of your fields and types, for instance for Player and Country:

ruby script/generate scaffold_resource Player full_name:string first_name:string last_name:string birth_date:date country_id:integer gender:string class:integer dead:boolean eligible:boolean

ruby script/generate scaffold_resource Country code:string name:string

rake db:migrate


My application will be RESTful and I will create a new database schema.

2 commentaires:

Chirag Patel a dit…

How did you use ActiveScaffold with the model that was generated by scaffold_resource? The instruction say to "empty out built in Rails scaffolding". I'm not sure what they mean by "empty out", but I just commented out all the methods that scaffold_resource generated. I got the following error when I click "Edit" on one of the rows of my ActiveScaffolded table:

ActionController::RoutingError (no route found to match "/user/1;edit"
with {:method=>:"get/"}):

any advice? thanks! chirag

Gaël Marziou a dit…

Hi Chirag, I did not do this I started again from scratch.