如何检查 SQL 在 Ruby 中使用 rake 在 Rails 中执行的命令

How to check SQL commands executed using rake in Ruby on Rails

我想计算用户对文章的投票数并将其保存在某个地方。

我想检查所有 SQL INSERTCREATE 当我们做类似的事情时执行的行:

>$ bundle exec rake db:reset
>$ bundle exec rake db:seed
>$ bundle exec rake test:prepare

有什么方法可以在 Rails 上检查 Ruby 中的 SQL 命令?

您可以添加自定义 Rake 任务并在需要记录 SQL 输出时使用它:

task log: :environment do
  ActiveRecord::Base.logger = Logger.new(STDOUT)
end

现在您可以运行:

bundle exec rake log db:reset
bundle exec rake log db:seed
bundle exec rake log test:prepare

参见“Is it possible to output the SQL change scripts that 'rake db:migrate' produces?