Reputation: 131
I am trying to run a ruby file that updates the database using the rake task . But overtime I run the code, I get an error saying : rake aborted! cannot load such file -- app/services/insert_data
The ruby file 'insert_data' is located under the app directory in a folder named 'services'
Here is the rake task I created to run it:
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.
Upvotes: 0
Views: 2073
Reputation: 454
Inside your task :test block try -
Rake.application.rake_require "#{Rails.root}/app/services/insert_data"
Upvotes: 0
Reputation: 5155
To be able to use Rails environment within a rake task you need to declare that:
namespace :record_generate do
task :test => :environment do |_, args|
ruby "app/services/insert_data"
end
end
Upvotes: 2
Reputation: 4907
I think you should try placing your app/services/insert_data
inside the script directory. I'm assuming that your ruby file is not a rails model with logic. I would try this.
Get your file and place it in the /script/
directory.
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.
Upvotes: 0