I\'m looking for something like CodeIgniter\'s:
$this->db->last_query();
(http://codeigniter.com/user_guide/database/helpers.html)
Probably because I have a newer version of ActiveRecord than when this question was asked, but to get it to work with ActiveRecord 3.2.3 I updated the script of Simone Carletti: I added the extra attribute binds and moved the call to alias_method_chain below the method definition because otherwise it would raise an error saying that it can't find the method.
ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
attr_reader :last_query
def log_with_last_query(sql, name, binds=[], &block)
@last_query = [sql, name]
log_without_last_query(sql, name, binds, &block)
end
alias_method_chain :log, :last_query
end
Getting the last query is still the same:
ActiveRecord::Base.connection.last_query # => ...