Ruby On Rails, round III: First real steps!

So I finally installed Ruby on Rails, and started playing around a bit with it!

It was quite easy with that tutorial I linked. And although I am not used yet to the Rails terminology, I am going to comment some things I liked:

  • It is meaningful. The models are real models, in the way that you specify their properties in their definitions, with no need to do weird tests and validations when manipulating them to demonstrate and keep the validity, as you do in php. For example, in php, if you want to have a 1:M relationship between two tables, you need to keep track of the uniqueness by yourself, helped by the database logic (although this case is really rare to find, unfortunately).

    Here in Ruby on Rails (RoR from now on) you just go to the model definition for A and say:

    has_many: table_for_B (was: has_many_and_belongs_to: table_for_B)

    And that way you specify that A has a 1:M relationship with B. More information in this example of an Access Control List.

    You can also have many to many relationships (M:M), which is specially useful for example in the case of crossed tables references. There's also a good tutorial for this topic here.
  • Another thing I liked is the naming. There's a convention about how to name things, so when a new programmer has to read code by other people, it's easier to catch up the idea than if you have to figure out if the table name for storing the users information is called Users, or User, or tbl_users, and so on.

    In the same style, no more wondering about how the primary key of a table is called. Id? table_id? cod? pk_id? Something simpler: id (absolutely lowercased).

    This not only helps the coders, but also the framework in itself. Seems like ActiveRecord (the subframework used for accessing database) uses that to link Models names and their persistence (i.e. the database).
  • Derived from the naming, comes the organization. All is organized always in the same fashion. When you first create the project (using a helper script), it creates all the files and folder structure for it. So you won't have a new programmer trying to reinvent the wheel and putting stylesheets in a css folder, instead of the old good stylesheets that you were using. Or img instead of images. And so on.
    Consequently and as you could expect, the models are in one folder, the controllers in other, etc...

  • And finally (by the moment) what is good is to find a nice community of people trying to learn and collaborate with others, sharing their pieces of code and wanting to understand how it works, not simply copying and pasting and expecting it to work. For example, if you go to the many to many relationships tutorial that I commented, you'll see how the people read the tutorial, and found errors while trying to put it into practice. And then they fixed the errors and let know the author so he can add the fixes to the original tutorial. Isn't it nice?

    I like the fact that there are not many script kiddies on this RoR scene, as they are in php. Call me elitist but I sometimes think computers are not for everybody.
More to come in next chapters ;)

UPDATE: An interesting comparison between J2EE and Ruby On Rails. If you have worked with J2EE, you'll remember nightmares as the controller mappings, the need for cleaning the cache if tomcat didn't detect properly you changed your code, etc... Take a look here and see what can be done with Rails which is overkilling if made with j2ee :-)