How to find the last SQL statement executed in ActiveRecord


#1

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.


#2

you can always save that for your reference, but why do you need it?
there
may be a better solution for the problem


#3

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.


#4

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!


#5

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_record_io_mode.0.0.1.tgz

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.

#6

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_record_io_mode.0.0.1.tgz

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.