Saturday, November 3, 2007

Ruby on Rails

As internship project for my thesis of applied computer science I developed a web application with Ruby on Rails. Since that point I am a big fan of Ruby on Rails and the used concepts such as Active Record, MVC, database migrations, scaffolds, unit- and function testing and many more. If you find some time I would stronlgy suggest to give it a try because it makes web development fast, easy and agile. "Simple" web pages can be created in a very small amount of time; but Rails provides also features for complex pages. This requires some time of vocational adjustment but then it is almost fun to create dynamic web pages.

Active Record
The most impressing thing of Rails was the built in support for the Active Record pattern. This is the short definition of what it is:
An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.
Martin Fowler
The idea behind is very very interesting. It mainly states that you take care of building the model with its objects and the relations between them and Active Record maps then automatically to an underlying database management system. The big advantages of this are that you can change the database when you like because you are not bounded to a specific DBMS; another big advantage is that you do not have to write any SQL which reduces errors and safes you a lot of code and maybe also a class which is called DBManager ;-).
To start you simply "tell" Active Record which is the database that you use, the name of the catalog and the user with which to connect to the DBMS. After that you create so called migration scripts which are written in Ruby and are used to create the needed tables in the database; you can also create the database tables manually or with SQL but I found that this was the easiest way. After the successful creation of the tables you can start coding. It is very simple to do that as you can see by this example:

This code creates a new user and stores it in the database:
user = = "uname"
user.passwor = "mypassword"

This code updates an existing user and stores the changes in the database:
user = User.find(3) = "newName"

This code destroys a user:
user = User.find(3)

It is as simple as this and you do not have to write any SQL statements.

Finally some usefull links to get started with Ruby on Rails:
Ruby on Rails Home
One click Ruby installer
Tutorial (not tried by myself)

No comments: