Unfortunately in Rails 2.x this is actually quite hard. I've posted a similar question on Stack Overflow before and ended up digging deep into the source code of Rails to find a way. It just isn't architected in a way to allow this.
What I ended up doing was running the query in a transaction that I rolled back, and for the length of the transaction setting the logger to my own StringIO object that I could read after.
This is from memory but hopefully you understand it enough to adjust it if it doesn't work:
Model.transaction do
Model.logger = str = StringIO.new
Model.complex_scope.chained_complex_scope
Model.logger = ActiveRecord::Base.logger
str.rewind
str = str.read
# perform some regex on str to get the actual query
raise ActiveRecord::Rollback
end
It is ugly as hell and I never liked it (I wrapped it in a sql { Model. complex_scope.chained_complex_scope }
) but it kinda worked for me (I only used it in development though, so I had some tolerance for errors)