AFAIK, thereβs no easy way to access the list of queries. However, you can easily access them by creating a super simple registrar.
If you open the ActiveRecord::ConnectionAdapters::AbstractAdapter class, you will see a method called a log. This method is called for each logging request. By default, it logs all statements using the Rails logger.
You can do something like
ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do attr_reader :last_query alias_method_chain :log, :last_query def log_with_last_query(sql, name, &block) @last_query = [sql, name] log_without_last_query(sql, name, &block) end end
Now you can get the request with
ActiveRecord::Base.connection.last_query
Simone carletti
source share