views:

48

answers:

2

What's the code to look at the sql an ActiveRecord query is going to produce. For example, Modelname.all would produce "Select * from modelname". Is there any method that gives access to that string?

+1  A: 

I don't know if it can be done through an API, but you can see the generated SQL by looking at development.log, which I suppose you could parse.

Extending this answer, you can provide a custom Logger to ActiveRecord via

ActiveRecord::Base.logger = yourLoggerHere

Maybe you can create a Logger that'll do all the work for you. Found this link which implements one for detecting slow queries.

JRL
Thanks JRL, I'm actually looking specifically for how its done throug the API.
Daniel
A: 

There's no API method to store the queries executed by ActiveRecord. However, you can hack ActiveRecord, as explained here.

Simone Carletti