Oh yeah. That’s why I hate IRC.

A couple of days ago I took it upon myself to give IRC a whirl for the first time in years. Back in my Quake days, IRC was on 24/7 and it was my main mode of communication. Back then, I was 15.

Looks like everyone is still 15 on IRC. I’ve been hanging out lately on freenode, mostly in #vim and #rubyonrails, and I have to ask, why can’t you either just shut the fuck up or answer a question? 50% of questions get answered, while the other 50% either get lectured or get ridiculed. Ugh. Today it finally happened to me. I was asking about how to set up Rails routes for POST requests to the public/ folder. Immediately I got something similar to ‘why r u doing that lol’, at which point I explained that Facebook was doing it, at which point I got another idiotic response, and blah blah… it just went downhill from there. The worst part is that the guy was sort of right in the answer he gave, but I was too incensed by his behavior to acknowledge that.

Three minutes later, he posted this video: http://video.google.com/videoplay?docid=-2987243478639579087. Everyone was LOLing and ROFLing. In the #rubyonrails chatroom. Sigh.

At least in shit-talking Quake days, we backed up our words with our skills. Matches decided who would shut up and who could run their mouth. There’s no such resolution in a programming chat room. Looks like IRC is going to stay closed again for a very long time.

Don’t call super() in any custom find_by_x methods in your ActiveRecord classes

I recently spent some time pulling my hair out over some custom find_by_x methods I had written for some of our models. It looked something like this:

class PostalArea < ActiveRecord::Base
  def self.find_by_postal_code(code)
    super(code[0..2])
  end
end

I was building a database of Canadian postal areas, indexed only by the first 3 characters of the postal code. The idea was that the model would take care of trimming off the other characters in the postal code before calling ActiveRecord’s magic find_by_postal_code method.

Well, it turns out that it’s a little too magic. Of course, find_by_x methods are handled in ActiveRecord’s method_missing method. If you look at the code, these custom finders are hard-coded into your class after the first time they are called. Super Ruby stuff. In other words, my find_by_postal_code method in the PostalArea class was getting clobbered by ActiveRecord after calling super() just once. PostalArea.find_by_postal_code(”M4C 1B5″) would return the record indexed by “M4C” on the first call, but subsequent calls returned nil. It was calling ActiveRecord’s custom finder, not my own.

Instead of calling super() in an overloaded find_by_x function, call your find() method instead.

Yo.

Hi there.