8
votes

Rails WEBrick shows raw SQL statements for any ActiveRecord activities. How to enable that in the console?

3

3 Answers

12
votes

To do this you have to enable logger, you could do this as follows.

Open the rails console:

ActiveRecord::Base.connection.instance_variable_set :@logger, Logger.new(STDOUT)

Have a look at this link:

http://rubyquicktips.com/post/292826666/display-activerecord-generated-sql-queries-in-the

3
votes

A similar way to achieve this without resorting to digging into ActiveRecord internals and using instance variables is to just access the config object that Rails gives you. Place this inside config/application.rb:

config.logger = Logger.new(STDOUT) if($0 == 'irb' || $0 == 'script/rails')
1
votes
  • Go to your console.rb location /lib/rails/console.rb

  • Look for ActiveRecord::Base.connection.instance_variable_set

  • Change it to following

ActiveRecord::Base.connection.instance_variable_set :@logger, Logger.new(STDOUT)