Forum: Ruby on Rails How can I retrieve the SQL during each model action?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
381b73832cca073177d26a3204e13c69?d=identicon&s=25 xeon (Guest)
on 2009-02-13 15:13
(Received via mailing list)
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2009-02-14 12:00
(Received via mailing list)
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
69112da010aa0192e775a4236cd9af3a?d=identicon&s=25 Xuan (Guest)
on 2009-02-16 13:55
(Received via mailing list)
On Feb 13, 12:59 pm, xeon <xeo...@gmail.com> 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, ....
381b73832cca073177d26a3204e13c69?d=identicon&s=25 xeon (Guest)
on 2009-02-17 10:32
(Received via mailing list)
Thanks for the valuable reply. But I found myself most comfortable
with:
1) Netbeans debugging
2) ruby-debug (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!
This topic is locked and can not be replied to.