[英]rake aborted! cannot load such file Ruby on Rails
I am trying to run a ruby file that updates the database using the rake task . 我正在尝试运行一个ruby文件,该文件使用rake任务更新数据库。 But overtime I run the code, I get an error saying : rake aborted!
但是加班我运行代码,却收到一条错误消息:rake中止了! cannot load such file -- app/services/insert_data
无法加载此类文件-app / services / insert_data
The ruby file 'insert_data' is located under the app directory in a folder named 'services' 红宝石文件“ insert_data”位于应用程序目录下名为“ services”的文件夹中
Here is the rake task I created to run it: 这是我创建用来运行它的rake任务:
require 'app/services/insert_data'
namespace :record_generate do
task :test do
ruby "app/services/insert_data"
end
end
Please help in removing this error. 请帮助消除此错误。
To be able to use Rails environment within a rake task you need to declare that: 为了能够在rake任务中使用Rails环境,您需要声明:
namespace :record_generate do
task :test => :environment do |_, args|
ruby "app/services/insert_data"
end
end
I think you should try placing your app/services/insert_data
inside the script directory. 我认为您应该尝试将您的
app/services/insert_data
放在脚本目录中。 I'm assuming that your ruby file is not a rails model with logic. 我假设您的ruby文件不是具有逻辑的Rails模型。 I would try this.
我会尝试的。
Get your file and place it in the /script/
directory. 获取您的文件并将其放在
/script/
目录中。
Place these line of code at the top of your file 将这些代码行放在文件顶部
#! /usr/bin/env ruby
require_relative "../config/environment"
Now on your command line you can do execute this: 现在,您可以在命令行上执行以下操作:
rails runner name_of_your_file
That is one way of doing it. 那是做到这一点的一种方式。 If you specifically need to use a rake task I can try posting an answer for that too.
如果您特别需要使用rake任务,我也可以尝试发布答案。
在您的任务中:测试块尝试-
Rake.application.rake_require "#{Rails.root}/app/services/insert_data"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.