→ .railsrc

10 March 2008

I like to keep a .railsrc file in my ~ for Rails-specific IRB goodies.

In your ~/.irbrc:

load File.dirname(__FILE__) + '/.railsrc' if $0 == 'irb' && ENV['RAILS_ENV'] 

All set. Here’s an evily awesome snippet to get you in the mood:

def method_missing(method, *args, &block)
  User.find_by_login(method.to_s) || super
end

Now:

>> defunkt
=> #<User id: 44, login: "defunkt", ...>

Lovely. I also like this one:

def sql(query)
  ActiveRecord::Base.connection.select_all(query)
end