Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show SQL statements in Rails console like WEBrick?

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

like image 768
ohho Avatar asked May 24 '12 04:05

ohho


3 Answers

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

like image 152
sameera207 Avatar answered Oct 04 '22 02:10

sameera207


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')
like image 41
Cody Caughlan Avatar answered Oct 04 '22 02:10

Cody Caughlan


  • 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)

like image 20
Srinivas M.V. Avatar answered Oct 04 '22 00:10

Srinivas M.V.