Rails - 如何在特定时间自动将记录从 table 转移到 table?

Rails - How to auto-transfer records from table to table at a specific time?

我正在 table A 中预先存储记录,我想将这些记录从 table A 转移到 table B 在特定时间自动,比如说每天晚上 08:00 PM。

关于如何解决这个小问题有什么想法吗?

您可以创建 rake 任务来执行您的工作,然后使用 cron(默认 *nix 时间管理器)安排它。它的语法很难记住,所以我更喜欢使用 Ruby 包装器,gem whenever.

您可以使用 whenever gem 来 运行 cron 作业...例如每 5 分钟 运行s 的作业

在schedule.rb

every 5.minutes do
rake "transfer_data:send_data"
end

lib/tasks/send_data.rake

 #!/usr/bin/env ruby

namespace :transfer_data do
  desc "Rake task to transfer data
  task :send_data => :environment do
## code to transfer data from one table to other table 
end
end

使用bundle exec rake transfer_data:send_data

执行任务