Forum: Ruby on Rails How to find the last SQL statement executed in ActiveRecord

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.
Chris S. (Guest)
on 2006-04-20 17:50
Is there a method for retrieving the last SQL statement executed by
ActiveRecord?  I realize I can open the log file, but I'm looking for a
programmatic way.
Emin H. (Guest)
on 2006-04-20 18:25
(Received via mailing list)
you can always save that for your reference, but why do you need it?
there
may be a better solution for the problem
Chris S. (Guest)
on 2006-04-20 18:29
Emin H. wrote:
> you can always save that for your reference, but why do you need it?
> there
> may be a better solution for the problem

I'm building a new ActiveRecord 'Acts' module, and when running the unit
tests, I'd like to output the queries that are being build to ensure
that the options I provide are working.
Leon L. (Guest)
on 2006-04-20 18:43
(Received via mailing list)
ActiveRecord::Base.logger = Logger.new(STDERR)

ActiveRecord::Base.colorize_logging = false

On 4/20/06, Chris S. <removed_email_address@domain.invalid> wrote:
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Never be afraid to try something new. Remember, amateurs built the
ark; professionals built the Titanic!
Jean-François (Guest)
on 2006-04-20 18:49
(Received via mailing list)
Hello Chris,

> Emin H. wrote:
> > you can always save that for your reference, but why do you need it?
> > there
> > may be a better solution for the problem
>
> I'm building a new ActiveRecord 'Acts' module, and when running the unit
> tests, I'd like to output the queries that are being build to ensure
> that the options I provide are working.

Maybe you can have a look at Zach D.' plugin :

"Here is a link to 0.0.1 of the ActiveRecord IO Mode plugin:
 http://www.mktec.com/oss/rails-plugins/active_reco...

It is more documentation then code, and only 201 lines. It works
successfully with the MysqlAdapter. I believe it should work for
Postgres, but I'm not a Postgres user so any feedback would be great.
Any Oracle, MSSQL, SQLite, etc... users please let me know if this works
or not for you."

    -- Jean-François.
Chris S. (Guest)
on 2006-04-20 19:27
Jean-François wrote:
> Hello Chris,
>
>> Emin H. wrote:
>> > you can always save that for your reference, but why do you need it?
>> > there
>> > may be a better solution for the problem
>>
>> I'm building a new ActiveRecord 'Acts' module, and when running the unit
>> tests, I'd like to output the queries that are being build to ensure
>> that the options I provide are working.
>
> Maybe you can have a look at Zach D.' plugin :
>
> "Here is a link to 0.0.1 of the ActiveRecord IO Mode plugin:
>  http://www.mktec.com/oss/rails-plugins/active_reco...
>
> It is more documentation then code, and only 201 lines. It works
> successfully with the MysqlAdapter. I believe it should work for
> Postgres, but I'm not a Postgres user so any feedback would be great.
> Any Oracle, MSSQL, SQLite, etc... users please let me know if this works
> or not for you."
>
>     -- Jean-François.

Surely there is something in ActiveRecord itself to get this for me.
What I want is to be able to say something like:

ActiveRecord::Base.connection.last_sql

Again, I know I can use the logger, but I don't want EVERY query - just
the most recent one that AR "built" based on a find(...) or whatever.
This topic is locked and can not be replied to.