Nicolas Alpi, Web developer

A blog about productivity, startups and me.

10 Things I've Learned Developing in Rails

I’ve started learning Rails and Ruby 3 years ago now, and now all my side projects are made in RoR.
I’ll be ready soon to release my third rails application, but having launched earlier this year serialcooking.com I’ve learn a lot on good RoR good practice in terms of development, deployment and maintenance.
I just wanted to share some of my learning/mistakes over the time.

Manage your queries properly

Believe it or not, but before the beta launch of serialcooking.com, my index page was performing 95 queries! Outch. I wanted to write my code as fast as possible and completely forgot to do performance review.

Here were my mistakes:


  1. I wasn’t using :joins and :include properly (See FIND doc on apidock)

  2. I wasn’t using query_reviewer (Query reviewer on googlecode)

Query Reviewer is a fantastic tool, helping you to resolve query numbers and table indexes problem in development mode. Worth try it!
h2. Assets management

The less you transfer, the fastest your page will load.

So instead of linking to my server Jquery file, I used the Google Ajax API, which is available for various JavaScript libraries.

Another interesting tool to package your assets in rails is the asset packager plugin. (Asset packager on github)

Basically, you declare in one config file all your assets to package, and on your staging/production server it will only transfer 1 packaged file per type (CSS/JavaScript …). The gain is just fantastic, and it’s so easier to maintain.
h2. I’ll never use HTML again

Forget about HTML and ugly coded view. Welcome to HAML!. (HAML/SASS website).

I must admit, I came to HAML late, doing resistance and loving my XHTML. But I give it a real try on a side project. And the fact is, it’s just wonderful!

Judge by yourself

<div id="profile">
<div class="left column">
<div id="date"><%= print_date %></div>
<div id="address"><%= current_user.address %></div>
</div>
<div class="right column">
<div id="email"><%= current_user.email %></div>
<div id="bio"><%= current_user.bio %></div>
</div>

</div>

Will become in HAML

#profile
.left.column
#date= print_date
#address= current_user.address
.right.column
#email= current_user.email
#bio= current_user.bio

The learning curve is really quick. My only regret is to not have tried it before!.
h2. Never, never render HTML in your Rails Helpers

This is something I did a bit in the beginning. I quickly went back. Rendering HTML in your helpers is a very bad idea, rendering a partial instead is a better one ;)
h2. Never use RJS again

For my first Rails project, I used RJS. (It was 2 years ago). At the time, it was fantastic, quick bla bla bla. Well, today, with current Javascript practices and Javascript libraries I don’t see the point of having ugly and obtrusive JavaScript in my code.

I prefer using Jquery like in this previous post. So easier to maintain.
h2. Maintain and deploy your code smarter

I use git and github to manage my projects code. I use Capistrano and Capistrano-ext combined with the github repository to deploy my code and I use Passenger on Apache2 to access my application.

I also ALWAYS use a staging environment, just to be sure that everything is ok before moving to production.
h2. Don’t believe in anyone, test!

I now test my code for 1 year. I first started with the embded Rails testing framework, and then, because all the cool kids were loving it, I migrate to Rspec during 3 months. I didn’t like it, but because everyone liked it I force myself to continue!

Finally I was writing bad quality tests, and didn’t enjoy my tests.

I then came over shoulda and now I enjoy again writing my tests.

The point here is, and it’s the same when you choose Rails over Django or Symphony, don’t trust anyone because he says “this is the better plugin/technology/language ever”.

Instead, try by yourself, see what the others can brings to you, and have a clear in mind choice.
h2. Monitor your application

Using a tool like Newrelic is a real benefit on monitoring what’s happening in your web app, in real time. Find the slowest part of it and fix it.
Just fantastic. Currently I just use the free version, but I’m considering on upgrading as it’s so useful.
h2. Never let an error blocking your users

I use Hoptoad to catch all the errors happening on my apps. As newrelic, you just install the plugin, and it works like a charm. You receive an email when an exception is raised in your app, so you can resolve it as fast as possible.
h2. The tail command

A very useful command that I just learned months ago.

$ tail -f log/development

And leave your terminal window open. It will refresh every second with new thing coming from your log. Really useful, I don’t remember how I was doing before that!.

What about you?

Do you see something that you learned and is not present here? Just add it in comment.

Comments