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. His old blog is still available if anybody cares.
Feel free to ask me stuff or tell me that I suck.
A rundown on the various Ruby VM’s being developed.
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:
It looks for those stuff in
Location header (attempts to make a HEAD first and goes to GET if it doesn’t found something)<link> tags in the bodyCode is available at http://github.com/jou/yaurls/tree/master and is under a MIT license.
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.