Ruby on Rails from the perspective of a Flex developer

A little while ago I decided to take a look at Ruby on Rails. I built a small application and thought it may worthwhile to share my impression of the framework.

The great thing about Rails (and most other MVC implementations) is that assuming you’ve worked with MVC before you already know how it all fits together. The main challenge we face as coders is managing complexity, the MVC pattern is a perfect fit for Web based applications.

While Rails is similar to other MVC frameworks, it certainly has its unique aspects. One of my favorites is the concept of migrations. Migrations allow you to tie your database changes directly to your code changes. It becomes even more useful when used with Capistrano (a incredible deployment tool). In most other projects I’ve worked on I’ve needed to keep track of my database changes, when it’s time for release these changes are then run on the production database. Migrations give you a formal way to solve this problem. In addition they make it possible to roll back schema changes if for example you decide to back out a deployment. With Capistrano, deploying to production just takes a single command and rolling back is just as easy.

Another great Rails feature is ActiveRecord. This is essentially an Object Relational Mapper (ORM) which means you don’t need to write SQL queries to manage your data. You can simply manipulate objects and ActiveRecord handles updating the database. Since Flex apps can’t directly manipulate the data in your database, using Rails as a server-side technology with a Flex front end works really well. Additionally, there are many Gems (Rails plugins) available which enable you to (among other things) extend the ActiveRecord approach to other datasets. For example, the Gmail and SugarCRM gems enable you to access the respective datasets with just a few lines of code.

Finally, the Rails framework provides a ton of useful functions for everyday tasks. A good example of this is the pluralize function. It takes a string an determines the correct grammar depending on the number of items. ie, “1 record” vs “2 records”. Here’s a simple ActionScript function to provide a similar functionality, although it won’t be able to handle a 1 person/2 people.

public static function pluralize( string:String, count:int ):String
{
	string = string.replace( "?", count );
	return count == 1 ? string : string + "s";
}

// sample usage
StringUtils.pluralize( "? record", data.length );

The Ruby language itself is entirely object oriented which is really nice, but I struggled to switch back to a dynamic language. Years ago I loved the flexibility it provided but I’ve grown accustomed to the ease with which I can refactor variable/function names in Flex. It’s said that static languages just check that your code is syntactically correct while dynamic languages with unit tests can check that your code is logically correct. This may be true but I’m willing to bet there are quite a few Rails projects out there without many unit tests.

The main problem I ran into was actually related to hosting. Rails performance has come along way but the challenge I faced was the start up times. The hosting provider I used (I imagine many are the same) would shut down the Passenger server if the app wasn’t used for 5 minutes. Start up takes a good couple of seconds which meant for a site with little traffic it appeared to have horrible performance. I worked around this by having a cron access the site every couple of minutes but this obviously isn’t ideal.

Rails alone clearly can’t complete with Flex in terms interactivity but when paired with JQuery UI or another JavaScript framework there’s hope. I believe we’re still years away from an HTML equivalent to Flex but only time will tell… who knows, maybe the Falcon JS compiler will one day take flight.