O RLY?

YA RLY! NO WAI!

O HAI! This is some of the random ramblings of Jiayong Ou, a generic geek and web developer. Most of his ramblings are on twitter as @jiayongou. More on his other online activities are on his other page. His old blog is still available if anybody cares.

Wednesday, 25th November 2009
It is tagged with
Friday, 28th August 2009
It is tagged with
Monday, 17th August 2009

srs.li now supports rel=”shorturl” and friends

Finally got around to implement support for rel="shorturl" and friends in srs.li, my own URL shortener (yes, I know I’m late :-).

At the moment it supports:

  • `rel=”(shorturl|shorturi|shortlink)”
  • `rev=”canonical”

It looks for those stuff in

  • HTTP Location header (attempts to make a HEAD first and goes to GET if it doesn’t found something)
  • <link> tags in the body

Code is available at http://github.com/jou/yaurls/tree/master and is under a MIT license.

It is tagged with
Thursday, 12th March 2009
It is tagged with
Wednesday, 11th March 2009

ActiveRecord, named scopes, select statements and you

ActiveRecord’s named_scopes are awesome, but I recently run into something less awesome about it: It doesn’t merge the :select option of nested scopes.

So let’s look something like that:

class Profile::Business < Profile::Profile
  # ... some code ...

  named_scope(:by_popularity, 
    :select => "profile_profiles.*, count(profile_fans.member_id) AS fan_count",
    # ... some more stuff for the scope
    :order => "fan_count DESC"
  )

  # ... some more code ...
end

And we use the named scope like this:

Profile::Business.by_popularity.find(:all, :select => 'profile_profile.*')

Should work, shouldn’t it? But as you might have guessed, it’s where ActiveRecord is exploding by throwing an ActiveRecord::StatementInvalid exception:

Mysql::Error: Unknown column 'fan_count' in 'order clause':

If you look at the rails log, you should see something like that:

Profile::Business Load (0.017810) SELECT profile_profiles.* FROM `profile_profiles` INNER JOIN [... some more SQL ...]

Yes, my first though was ‘Dude, where’s my SELECT?’. Turns out ActiveRecord doesn’t merge them it just use the last :select it encounters. In my case the workaround was easy by moving the COUNT() from :select to :order:

class Profile::Business < Profile::Profile
  # ... some code ...

  named_scope(:by_popularity, 
    :select => "profile_profiles.*",
    # ... some more stuff for the scope
    :order => "count(profile_fans.member_id) DESC"
  )

  # ... some more code ...
end

Crisis averted in this case, but it still leaves an uneasy feeling. And of course, the documentation says nothing about that.

Oh well… Someone proposed a patch for this problem and let’s hope it makes it into a Rails release.