Ruby on Rails

Depends how you interpret things

Enhancing script/console


I Used Rails ./script/console a lot for debugging my rails apps. Every time when I started debugging something, I have to keep my console log[*.log] file to see what corresponding queries being generated to carried out the result I desired.

I found a better solution for that. I enhanced my script/console, which enabled on-screen query logging for my rails app. I made a small change to #{RAILS_ROOT}/script/console.rb file, which it will look to load any additional ruby file resides in #{RAILS_ROOT}/console_script/ if available.

I have also included the script I wanted to load in the first place, I find it quite handy to be able to see what kind of queries my commands are generating.

#updates /script/console.rb

LOAD_HOOK_DIRECTORY = “#{RAILS_ROOT}/console_scripts”

Find.find( LOAD_HOOK_DIRECTORY ) do |filename|
if filename =~ /\.rb$/
puts “Adding #{filename} to load-path”
libs << ” -r #{filename}”
end
end
#add sql_log.rb file to /console_script/
def log_to(stream=STDOUT, colorize=true)
ActiveRecord::Base.logger = Logger.new(stream)
ActiveRecord::Base.clear_active_connections!
ActiveRecord::Base.colorize_logging = colorize
end
log_to
About these ads

6 responses to “Enhancing script/console

  1. Rans 2010/08/19 at 8:00 am

    Nice one.. Thanks to @sandykrao :)

    the other option,

    ActiveRecord::Base.logger = Logger.new(STDOUT)

    Be sure to restart your console before running this command.

    and now try your statement like User.find(1)
    you can see some handy debug info. :-D

    • sandykrao 2010/08/19 at 8:08 am

      Thanks @rans, for your quick reply. Still If I’m doing it ActiveRecord::Base.logger = Logger.new(STDOUT) in my console. I have to run the same command every time to enable the logger.
      Thats why I uploaded that file with running ./script/console command, so that I don’t have to re-run it every time.

  2. Rans 2010/08/19 at 8:09 am

    I think it makes most sense to put it in development.rb and have logging always enabled for the console while developing.

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

  3. Jadwiga Babinski 2010/08/31 at 10:59 am

    This is a great post and may be one that ought to be followed up to see what happens

    A friend emailed this link the other day and I am desperately hoping for your next blog post. Continue on the first-class work.

  4. Edgardo Tomasulo 2010/09/01 at 4:19 pm

    Located your website via bing the other day and absolutely like it. Continue the truly great work.

  5. Natasha Swift 2010/09/13 at 3:55 am

    It was extremely interesting for me to read the post. Thanks for it. I like such themes and anything that is connected to this matter. I would like to read a bit more on this site soon. BTW, pretty nice design you have here, but don’t you think design should be changed every few months?

    Natasha Swift

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: