Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

SQL Display

#2799

I wrote a plugin called SQL Display http://github.com/radar/sql_display/tree/master a couple of days ago and since showing it off to some people, they've suggested that this should be in Rails core as it's helpful for debugging purposes. Admittedly, the code isn't "beautiful" and I'm sure someone can figure out a better way than I've done it but... I don't want to go into the effort of writing a patch for this and having it bounced. So who else thinks having this functionality in Rails is a good idea?

Reported by Ryan Bigg · June 13th, 2009 @ 11:18 AM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Bodaniel Jeanes
    Bodaniel Jeanes

    +1

    I've wanted this for a while...

    June 13th, 2009 @ 11:04 AM

  2. mark (at aussiev8.com)
    mark (at aussiev8.com)

    +1 i was just about to make a similar plugin.

    June 13th, 2009 @ 11:22 AM

  3. mib_s2d5o9
    mib_s2d5o9

    +1 this would make my life convenient - add to Core

    June 13th, 2009 @ 11:25 AM

  4. David Knorr
    David Knorr

    +1 However, I agree with you that it probably would be better to use a less hack-ish attempt. I don't know exactly how you'd implement this, but something like setting an instance variable in the sql method and then checking if it's true in whatever method in ActiveRecord that takes care of performing SQL queries, would be cool.

    June 13th, 2009 @ 12:33 PM

  5. Morten
    Morten

    Rails.logger.silence(Logger::DEBUG) do
    Account.count end

    The block returns the last statement of the block, which is expected behavior (or more intuitive at least). You get the SQL printed in your .log file, regardless of environment.

    To be desired would be nicer semantics on setting the log level for Rails.logger eg. at_level(level = Logger::INFO), and reimplement "silence" to call that with Logger::ERROR instead.

    June 13th, 2009 @ 06:24 PM

  6. Ryan Bigg
    Ryan Bigg

    Morten,

    Your code does not do what you say it does, for me it prints out the output of the count rather than the SQL.

    I've hacked some more today on sql_display and now I'm overriding methods in ActiveRecord in the plugin. I revoke my idea of putting this plugin into core, since it will involve too many if statements.

    June 14th, 2009 @ 10:08 AM

  7. Pratik
    Pratik
    • State changed from new to wontfix

    This should be in mailing list really. I just use 3 lines in my ~/.irbrc for this :

    def log_to(stream=$stdout)
      ActiveRecord::Base.logger = Logger.new(stream)
      ActiveRecord::Base.connection_pool.clear_reloadable_connections!
    end
    

    June 14th, 2009 @ 11:20 AM

  8. Morten
    Morten

    Ryan, I suppose I misunderstood your intention. You want the SQL in hand, I want to be able to log it regardless of environment.

    June 14th, 2009 @ 01:33 PM

  9. bingbing