Thursday, April 05, 2007

ActiveRecord::Base#find shortcut

from the { buckblogs :here } - Home by Jamis

I use ActiveRecord::Base#find a lot in the Rails console. A lot. As a result, I’ve started doing the following:

class < alias_method :[], :find
end

That little snippet saves me up to five entire keystrokes, every time I need to do a find!
prs = Person[5]
prs = Person[:first]
prs = Person[:all, :conditions => { :name => "Jamis" }]

And, thanks to the existing hash and array semantics, it loses none of find’s readability for most cases. Good stuff!

No comments :