我在寻找类似codeIgniter的:
I'm looking for something like CodeIgniter's:
$this->db->last_query();
(http://$c$cigniter.com/user_guide/database/helpers.html)
AFAIK,有没有简单的方法来获取查询列表。但是你可以很容易地访问它们创建一个超级简单的记录。
AFAIK, there's no easy way to access the list of queries. Nonetheless you can easily get access to them creating a super simple logger.
如果您打开类的ActiveRecord :: ConnectionAdapters :: AbstractAdapter
,你会看到一个名为日志的方法。这种方法被调用的每一个查询记录的声明。默认情况下,它会记录与Rails记录器中的所有语句。
If you open the class ActiveRecord::ConnectionAdapters::AbstractAdapter
you'll see a method called log. This method is invoked on each query to log the statement. By default, it logs all the statements with the Rails logger.
您可以这样做
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 query with
ActiveRecord::Base.connection.last_query # => ...
上一篇:批量插入记录到活动记录表批量