# Rails3 console : how to get actual sql statements

**URL:** <https://www.ruby-forum.com/t/rails3-console-how-to-get-actual-sql-statements/187906>\
**Category:** Rails\
**Created:** [May 1, 2010, 1:05pm UTC](https://www.ruby-forum.com/t/rails3-console-how-to-get-actual-sql-statements/187906 "2010-05-01T13:05:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nadal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6f9a4e/32.png) [@Nadal](https://www.ruby-forum.com/u/Nadal)\
**Post date:** [May 1, 2010, 1:05pm UTC](https://www.ruby-forum.com/t/rails3-console-how-to-get-actual-sql-statements/187906/1 "2010-05-01T13:05:29Z")

</div>

In rails 2.3.5 world I get to see all the sql statements on my  
console. Thanks to following line of code in my ~/.irbc

if ENV[‘RAILS\_ENV’]  
Object.const\_set(:RAILS\_DEFAULT\_LOGGER, Logger.new(STDOUT))  
end

However above code does not do its magic in rails3 world.

In rails3 what do I need to do to see sql statements in my rails  
console.

---

<div class="post-metadata">

**Author:** ![Nadal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6f9a4e/32.png) [@Nadal](https://www.ruby-forum.com/u/Nadal)\
**Post date:** [May 1, 2010, 7:16pm UTC](https://www.ruby-forum.com/t/rails3-console-how-to-get-actual-sql-statements/187906/2 "2010-05-01T19:16:34Z")

</div>

All I know is that RAILS\_ENV is now Rails.env and RAILS\_DEFAULT\_LOGGER  
is now Rails.logger

Maybe this helps you to find the solution 🙂

Other than that I usually open a second Terminal window and do a

tail -f log/development.log

there I see the SQL statements which I evoked from the console.  
Usually even color coded.

---

<div class="post-metadata">

**Author:** ![Nadal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6f9a4e/32.png) [@Nadal](https://www.ruby-forum.com/u/Nadal)\
**Post date:** [August 18, 2010, 3:33pm UTC](https://www.ruby-forum.com/t/rails3-console-how-to-get-actual-sql-statements/187906/3 "2010-08-18T15:33:13Z")

</div>

Here there is a tip:

> **[Display ActiveRecord generated SQL queries in the console](https://rubyquicktips.com/post/292826666/display-activerecord-generated-sql-queries-in-the)**
>
> If you want the console to display the SQL query that ActiveRecord executes just do the following (before you do anything else in the console): $ script/console \>\> ActiveRecord::Base.logger =...

In order to make it work for both Rails 2 and Rails 3, my ~/irbrc looks  
like:

require ‘logger’  
if ENV.include?(‘RAILS\_ENV’)&&  
!Object.const\_defined?(‘RAILS\_DEFAULT\_LOGGER’)  
Object.const\_set(‘RAILS\_DEFAULT\_LOGGER’, Logger.new(STDOUT))  
else  
ActiveRecord::Base.logger = Logger.new(STDOUT)  
end

…not sure though if there is a better way to do that… bye.

---

<div class="post-metadata">

**Author:** ![Nadal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6f9a4e/32.png) [@Nadal](https://www.ruby-forum.com/u/Nadal)\
**Post date:** [August 20, 2010, 12:09am UTC](https://www.ruby-forum.com/t/rails3-console-how-to-get-actual-sql-statements/187906/4 "2010-08-20T00:09:57Z")

</div>

(For Rails 3)

I have this in my development.rb environment file:

#log ActiveRecord  
ActiveRecord::Base.logger = Logger.new(STDOUT) if defined?  
Rails::Console
