How to show SQL statements in a Rails console like WEBrick?

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

+8
ruby-on-rails
source share
3 answers

To do this, you need to enable the registrar, you can do it as follows.

Open the rail console:

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

Take a look at this link:

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

+12
source share

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

 config.logger = Logger.new(STDOUT) if($0 == 'irb' || $0 == 'script/rails') 
+3
source share
  • Go to console.rb location /lib/rails/console.rb

  • Look for ActiveRecord::Base.connection.instance_variable_set

  • Change it to the next

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

+1
source share

All Articles