Monthly Archives: October 2008


One thing about having a blog hosted at (or on any other 3rd party website, for that matter) is that you shouldn’t have to deal with updates and the likes.

The downside, however, is that you are not able to do anything when things start to suck. I, for example, experience a pretty long waiting to get on this blog (and some others too, that are hosted at So please be patient with that website if it doesn’t show up in an instant. I can’t do nothing about it, I am sorry.

And no: I am not willing to pay my bucks just to be able to edit the CSS code. To be honest this is something I would expect off a free service already, but then I might be biased. Performance guarantees, that is a different breed altogether. Think about that,!


0x08: 10, 9, 8, … zero in on <what> ?

Rails – or to be more precise – ActiveRecord scopes can be a huge time saver. That is, unless you have a number of multiple conditions that stack onto each other. But if you are in need of an “advanced search” feature you will likely end up with something like the following:

condition1 = first_name && { :conditions => [ "first_name LIKE ?", "#{first_name}%" ] }
condition2 = last_name && { :conditions => [ "last_name LIKE ?", "#{last_name}%" ] }
condition3 = min_age && { :conditions => [ "birthday < ?", - min_age.years ] }

with_scope :find => condition1 do
  with_scope :find => condition2 do
    with_scope :find => condition3 do
      User.find :all, ...

That certainly is ok enough, but what I don’t like here is the wrapping of all those with_scopes into each other. What I want instead would look more like this

scope = User.scope 
scope.conditions "first_name LIKE ?", "#{first_name}%"    if first_name
scope.conditions "last_name LIKE ?", "#{last_name}%"      if last_name
scope.conditions "birthday < ?", - min_age.years if min_age
scope.find :all, ...

Not only is this five lines of code compared to eleven. I even think this expresses more clearly what is going on here: namely that we stack a number of conditions onto each other. Sadly the ActiveRecord interface doesn’t allow for something like that. But with Ruby so flexible it is easily possible to chew up the original interface and come up with something that matches my intent. And while we are at it: this way we can work around some bug limitation that occurs when stacking with_scopes: ActiveRecord doesn’t like multiple :join parameters to scopes. Or at least it didn’t like them…

So this is the code. I hope I managed to to without any typos… Have a great week!

Recommended reading

You want me to add a link here? Just write a comment…


Now, last monday I promised to rework the bodywork code into a plugin, “tested and all”. Some clashes with git later I finally managed to push it to github, you’ll find it at github. It even comes with a nice … Continue reading


One cool Rails feature – however undervalued – are after-filters. You might ask: What is the point of doing things after I am already done doing all things? Well, to prevent micromanagement: Sometimes you just want to do things all … Continue reading


0x05: SQL instruments

There are times, when you build a really complicated web page. Take a page like this page has more than eight parts that are to be taken straight from the database (assuming you are not logged in and get … Continue reading