Archive for January, 2007

Hobo – And you thought Rails made life easy!

January 29th, 2007

If you’ve seen anything of Ruby on Rails, you know it makes your life really easy with all its generators and plugins. Well, check out Hobo!

Hobo is a Rails plugin that adds tons of extra features that make your live even easier. Here’s a quote from their site:

Ruby: Sort an array of objects by an attribute

January 28th, 2007

In this example I’ll show you how easy it is to sort an array of (the same kind of) objects by an attribute. Let’s say you have an array of User objects that have the attributes ‘name’ and ‘login_count’. First, find all users.

d919902ae5ceb908e9c910963b35d062000

Now, we have to sort this array by ‘name’. Since we don’t know if any user used capitals in his name or not, we use ‘downcase’ to sort without case sensitivity.

A small not. ’sort’ returns a new array and leaves the original unchanged. You may want to just reorder the @users array, so use the ’sort!’ method. The ‘!’ indicates it’s a destructive method. It will overwrite the current @users array with the new sorting.

d919902ae5ceb908e9c910963b35d062001

That’s all! Since strings are comparable, this will sort you user objects alphabetically by name. Want to sort on login_count instead?

d919902ae5ceb908e9c910963b35d062002

So, now you can easily sort any object in an array just like you want it too!

Rails: Nested resource scaffold

January 23rd, 2007

In my previous post I told you about the resource scaffold. What you’ll be doing a lot is nesting these resources. Ingredients in recipes, comments on posts, options for products. You name it, you nest it!

Since Rails does not automatically nest resources for you, you should do this yourself. This is, with some minor tweaks, really easy to accomplish. In this example I’ll create recipes that have multiple ingredients.

I assume you have Rails 1.2.1 installed for this tutorial to work properly.

New in Rails: Resource Scaffold Generator

January 23rd, 2007

Oh boy! Rails 1.2 is all about resources. A product entry in your application is not just a rendered HTML page, but it “is” data. Rails 1.2 allows you to add a .xml extension to your url to retrieve the same product information in XML format!

Now, this all sounds hard. Two ways of rendering the same action depending on an extension. Generating XML code for every model in your database. No way you want to spend time developing all that stuff.

Well, Rails wouldn’t be Rails if there weren’t a generator for it!

Updates: Wordpress 2.1, Themes and Social

January 23rd, 2007

You can’t really see it, but Ariejan.net has been upgraded to Wordpress 2.1. I’ve been running 2.1 beta’s on a private server for some time now, so there weren’t any surprises during the upgrade.

I’ve also updated the theme to something more stylish and sober. Google Ads are less annoying now and merge nicely with the content. I’ve also re-enabled the social bookmark links so you can quickly bookmark articles on Del.icio.us or Digg.

Hope you like the new style. Some minor tweaks will be applied the following days where needed. Please let me know your thoughts on ariejan.net!

Why Ruby Rocks – Convince your fellow developers

January 19th, 2007

I often hear questions from my Java and PHP oriented friends about what makes Ruby so great and easy to use. Until today I’ve shown them some of my Rails feats (AJAX Scaffold always amazes people). Now, I came across this 20 minute Ruby introduction. Starting with the basic “Hello World” item, this article show step by step improvements to end up with blocks, objects and all that makes Ruby really worth while.

“Print this page” with Ruby on Rails

January 19th, 2007

You have put a lot of effort into creating a sexy overview of whatever data your application stores and allow your users to manipulate that data through AJAX controls. But, some people just want to print their data.

How to go about that? Just printing the page with data is generally not a good idea because it has been optimized for display on a screen. The first step we need to take is adapting our page for printing. Stylesheets are very handy tools for this. Check the following part of the header of my layout:

Rails: Group results by week (using group_by)

January 12th, 2007

The Enumerable class in Rails contains a method named ‘group_by’. This method is pure magic for a developer’s point of view. I’ll give you a simple example that shows the power of group_by.

Let’s say you have a table ‘posts’ containing blog posts. Now, you normally show these chronologically a few at a time. Nothing special there. For some special overview page, you want to group your posts by week.

With normal ActiveRecord operations this would be quite an elaborate task. But with group_by from Enumerable, it becomes child’s play.