# How can I retrieve the SQL during each model action?

**URL:** <https://www.ruby-forum.com/t/how-can-i-retrieve-the-sql-during-each-model-action/160265>\
**Category:** Rails\
**Created:** [February 13, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/how-can-i-retrieve-the-sql-during-each-model-action/160265 "2009-02-13T15:13:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/51bf81/32.png) [@xeon](https://www.ruby-forum.com/u/xeon)\
**Post date:** [February 13, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/how-can-i-retrieve-the-sql-during-each-model-action/160265/1 "2009-02-13T15:13:55Z")

</div>

Hi,

I would like to know for each running model action, how can I retrieve  
the SQL running behind? I don’t want to scan log each time for the sql  
running behind.  
E.g. post.find(:all,:conditions=\>{:id=\>‘1’}

I wanna debug the sql generated behind, is that other shorthand  
function like post.find().show\_sql() that enable  
us to view what’s SQL generated ?

Thanks

---

<div class="post-metadata">

**Author:** ![xeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/51bf81/32.png) [@xeon](https://www.ruby-forum.com/u/xeon)\
**Post date:** [February 14, 2009, 12:00pm UTC](https://www.ruby-forum.com/t/how-can-i-retrieve-the-sql-during-each-model-action/160265/2 "2009-02-14T12:00:58Z")

</div>

xeon wrote:

> Hi,
> 
> I would like to know for each running model action, how can I retrieve  
> the SQL running behind? I don’t want to scan log each time for the sql  
> running behind.  
> E.g. post.find(:all,:conditions=\>{:id=\>‘1’}
> 
> I wanna debug the sql generated behind, is that other shorthand  
> function like post.find().show\_sql() that enable  
> us to view what’s SQL generated ?

That sounds like a good idea. Perhaps a  
:log =\> :instance\_variable\_name option to find.

One alternative is the query\_trace plugin that displays  
a backtrace for each query, so that the log shows what  
line of code is associated with each query.

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![xeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/51bf81/32.png) [@xeon](https://www.ruby-forum.com/u/xeon)\
**Post date:** [February 16, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/how-can-i-retrieve-the-sql-during-each-model-action/160265/3 "2009-02-16T13:55:09Z")

</div>

On Feb 13, 12:59 pm, xeon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks

If you are using the console, you can type:

> > set\_logger\_to Logger.new(STDOUT)

An output example:

> > Page.find(:all, :conditions=\>{:id=\>1})  
> > SQL (0.000093) SET SQL\_AUTO\_IS\_NULL=0  
> > Page Load (0.000112) SELECT \* FROM `pages` WHERE (`pages`.`id` =

1. 

Page Columns (0.001215) SHOW FIELDS FROM `pages`  
=\> [#\<Page id: 1, …

---

<div class="post-metadata">

**Author:** ![xeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/51bf81/32.png) [@xeon](https://www.ruby-forum.com/u/xeon)\
**Post date:** [February 17, 2009, 10:32am UTC](https://www.ruby-forum.com/t/how-can-i-retrieve-the-sql-during-each-model-action/160265/4 "2009-02-17T10:32:16Z")

</div>

Thanks for the valuable reply. But I found myself most comfortable  
with:

1. Netbeans debugging
2. ruby-debug ([#54 Debugging with ruby-debug - RailsCasts](http://railscasts.com/episodes/54-debugging-with-ruby-)  
debug)
3. rdebug with textmate

That’s sum out my findings. Hope it helps. But, personally speaking,  
it will glad to have a shorthand function for developer to dump out  
the sql generated on the fly,  
as it effectively save time for browsing the log, run the debugger in  
console…cheers!
