toolmantim

Logging Rails activity in script/console

February 01, 2007 00:53 (Sydney Australia), updated 5 days later

Update: Check out System-wide script/console logging for something a little more convenient.

Just found this nice little nugget:


if "irb" == $0
  ActiveRecord::Base.logger = Logger.new(STDOUT)
end

Chuck that into environment.rb and you’ll get log messages straight into your script/console when interacting with your models. If there was a way to tell if you were in the rails console within .irbrc you could do it system wide.

Can anybody else see a tryactiverecord.com in the making? Why_, you hear that?

Comments

Chad Humphries

I do something like this in my irbrc:


script_console_running = (ENV.include?('RAILS_ENV') and IRB.conf[:LOAD_MODULES].include?('console_with_helpers'))
rails_running = (ENV.include?('RAILS_ENV') and !IRB.conf[:LOAD_MODULES].include?('console_with_helpers'))
irb_standalone_running = (!script_console_running and !rails_running)

To test it out yourself add the above and the below to your .irbrc file.


puts "script_console_running=#{script_console_running}" 
puts "rails_running=#{rails_running}" 
puts "irb_standalone_running=#{irb_standalone_running}" 

Tim Lucas

Sweet, thanks Chad! I’ve used this in my .irbrc and it works a treat.

To comment on this article you must have javascript enabled.