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:
- I wasn’t using :joins and :include properly (See FIND doc on apidock)
- 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!
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.
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!.
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
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.
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.
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.
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.
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.
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.
Nice post. I will try the query_reviewer and HAML. I would also suggest trying out the debugger gem ruby-debug. I couldn’t live without it
Hum, ruby-debug sounds nice, I’ll give it a try.
Would you care to elaborate on never rendering html in a helper? I use this trick and find it handy. All the other points make sense to me so I want to see if there is something I’m missing.
Do you also avoid rails built in helpers?
I certainly not avoid rails built in helpers! This is one part why Rails is so great!.
By do not rendering html in helpers I mean, instead of doing
def user_greetings(_user)
html = “
html += …
html += …
html += “
end
Or doing it content_tag …, I prefer using
def user_greetings(_user)
render :partial => “shared/greetings”, locals => {…}
end
The code is nicer, and If I need to reuse this piece of view, I can without repeating it.
Is it what you were looking for?
Some good tips, except I believe RJS are really cool! 99% people who complain about RJS templates just don’t understand how to write them properly. Also, HTML rendering in Helpers have their use. Most of the time you can do it really slick if you use content_tag and returning. Too much logic in the views makes them messy.
Yes, I agree RJS idea is really nice, but when you want to write unobtrusive JS code, it’s not the good solution.
.
What I don’t like with the current (Rails 2.X) RJS state, is the code they add on the view
Instead of tail -f, I generally use ‘less log/development’ then it’s a quick press of ‘F’ and less acts just like tail. The key difference is that if I see something important, I can press Ctrl+C and just page back and forth, search (’/’ + the term, ‘n’ to find the next occurance, etc.). It’s at great tool.
~Mike