# How to find the last SQL statement executed in ActiveRecord

**URL:** <https://www.ruby-forum.com/t/how-to-find-the-last-sql-statement-executed-in-activerecord/58317>\
**Category:** Rails\
**Created:** [April 20, 2006, 3:50pm UTC](https://www.ruby-forum.com/t/how-to-find-the-last-sql-statement-executed-in-activerecord/58317 "2006-04-20T15:50:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [April 20, 2006, 3:50pm UTC](https://www.ruby-forum.com/t/how-to-find-the-last-sql-statement-executed-in-activerecord/58317/1 "2006-04-20T15:50:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [April 20, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/how-to-find-the-last-sql-statement-executed-in-activerecord/58317/2 "2006-04-20T16:25:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [April 20, 2006, 4:29pm UTC](https://www.ruby-forum.com/t/how-to-find-the-last-sql-statement-executed-in-activerecord/58317/3 "2006-04-20T16:29:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [April 20, 2006, 4:43pm UTC](https://www.ruby-forum.com/t/how-to-find-the-last-sql-statement-executed-in-activerecord/58317/4 "2006-04-20T16:43:43Z")

</div>

ActiveRecord::Base.logger = Logger.new(STDERR)

ActiveRecord::Base.colorize\_logging = false

On 4/20/06, Chris S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Never be afraid to try something new. Remember, amateurs built the  
ark; professionals built the Titanic!

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [April 20, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/how-to-find-the-last-sql-statement-executed-in-activerecord/58317/5 "2006-04-20T16:49:57Z")

</div>

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](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.

```

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [April 20, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/how-to-find-the-last-sql-statement-executed-in-activerecord/58317/6 "2006-04-20T17:27:37Z")

</div>

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](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.
