10 October 2009

Using the Rails console

You can use the .irbrc file to make changes to the Rails console. My .irbrc file currenlty looks like this:

if ENV.include?('RAILS_ENV')
  require 'rubygems'
  require 'hirb'
  Hirb.enable
  if !Object.const_defined?('RAILS_DEFAULT_LOGGER')
    require 'logger'
    Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(STDOUT))
  end 
  puts "preferences loaded from ~/.irbrc. ActiveRecord logging is turned on."
end

The line towards the end is the important one for me. Without it, in six months I will probably have forgotten how I turned the logger on and even where the .irbrc file is. A problem I have with Rails: too much magic. And too many subtle ways to change its behaviour. These magic tricks are a wonderful thing, but my memory is is not good enough to retain them all.

See Ryan Bates’ Railscast on The Logger for a reference to the Rails logger trick.

Also, see Hirb for details of its lovely tabular output.

d. sofer