Friday, April 5, 2013

Temporarily Disable Executed SQL Command Output in Rails Console

When trying to get a nice, clean list of locations to copy/paste, I had the need to temporarily disable SQL command output in the Rails console.

Here's how I did it...

Run command with default ActiveRecord (AR) logger settings


>> Location.where('ancestry is null').size
   (0.6ms)  SELECT COUNT(*) FROM "locations" WHERE (ancestry is null)
=> 8888
>>

Disable AR output to console

Change the ActiveRecord::Base.logger.level setting

>> ActiveRecord::Base.logger.level = Logger::FATAL
=> 4


Now, when you run AR commands, you won't see the SQL commands that AR executed

ActiveRecord::Base.logger.level Options

  • Logger::DEBUG
  • Logger::INFO
  • Logger::WARN
  • Logger::ERROR
  • Logger::FATAL



>> Location.where('ancestry is null').size
=> 8888

Disable AR output to console via Ruby Console Config File

Add the following to your ~/.irbrc (or ~/.pryrc file):

ActiveRecord::Base.logger = nil


References

http://lexsheehan.blogspot.com/2013/04/ruby-string-interpolation.html





Sponsor Ads(Please visit one if you liked this article. Thanks!)

2 comments: