Friday, August 22, 2008

Fixnum to Models

I saw andand a while back and lol'ed at how clever and awesome it was. With that in mind, we added a couple methods to Fixnum to clean up record fetching on our more popular models:
Fixnum.class_eval do

  def to_user

That way when I find myself with something that might be a user id...
Maybe not something you'd want on every model, probably not optimized, but hey... it beats checking for nil, looking up the user id, checking for nil again and printing.


Anonymous said...

The second andand is not needed. User.find will raise an exception when there's no user with such an id.

Ben said...

You're absolutely right...

I meant to define to_user as using User.find_by_id(self) which would return nil.