我想在 lib 文件夹中创建一些脚本,这些脚本应该在我的数据库中插入/更新数据。我想使用我的模型,但是当我尝试使用它们时它们没有加载。这些脚本与应用程序无关,它们是 cronjobs。实现此逻辑的最佳行为是什么?提前致谢!
问问题
88 次
2 回答
1
如果您希望运行 cron 作业,您可能需要查看Delayed Job gem并将您的课程放入app/jobs
.
于 2013-11-05T08:27:49.593 回答
0
Just create a rake task under lib/tasks/my_db_task.rake When defining the rake task, make sure it depends on the "environment" one, which will load all the required dependencies you need to interact with your models (and all other classes, libraries usually available within your rails app)
desc "insert stuff in my db"
task populate_db: :environment do
10.times { User.create! name: "bob"}
end
And in you cron use something like the following (of course adapt to your environment):
* * * * * cd $RAILS_APP && RAILS_ENV=production /usr/local/bin/rake populate_db
于 2013-11-05T23:15:19.703 回答