rake aborted! cannot load such file Ruby on Rails - ruby-on-rails

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.

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

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.

Inside your task :test block try -
Rake.application.rake_require "#{Rails.root}/app/services/insert_data"

Related

Rails ci_reporter cannot load such file

I want to use ci_reporter to generate report xml, and then let jenkins publish error message.
I added this into gemfile
gem "ci_reporter"
Put following code into Rakefile
require 'ci/reporter/rake/rspec'
task :rspec => 'ci:setup:rspec'
Created a task file and put following code in lib/ci_report.rake
namespace :ci do
task :all => ['ci:setup:rspec', 'rspec']
end
Then typed rake ci:all in terminal, but it threw this error
rake aborted!
LoadError: cannot load such file -- ci/reporter/rake/rspec
What did I miss?
update
After searching, I still felt confused. However, I found anther gem called rspec_junit_formatter. It works perfectly!

rake db:seed is not working using neo4j gem

I have stated working on neo4j with rails using the gem 'neo4j', I want to seed some data in neo4j database. But whenever I am trying to do rake db:seed, it says
rake aborted!
Don't know how to build task 'db:seed'
I have checked all the rake tasks using rake -T, and there is no rake db:seed.
Does any one have any idea?
The Neo4j gem doesn't have a seed command. The command you're trying to use is provided by ActiveRecord. We'd love to add this functionality in, though, and if you'd like to help, we'd gladly accept a PR and/or contribute to the process. For now, open up an issue at https://github.com/neo4jrb/neo4j/issues and we can add it to the roadmap.
Finally got the solutions.
Create a file seed.rake under lib/tasks and put the code
namespace :db do
desc 'Load the seed data from db/seeds.rb'
task :seed => :environment do
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
load(seed_file) if File.exist?(seed_file)
end
end
Check apps root directory, is there a Rakefile?
Make a file with name "Rakefile" and enter these line
"#!/usr/bin/env rake
require File.expand_path('../config/application', FILE)
APP_NAME::Application.load_tasks
As others mentioned before, rails db:seed is a built-in feature backed by ActiveRecord. Assuming you are using neo4jrb without ActiveRecord, you won't be able to use this command to seed database any more.
However, you can use seedbank gem to recover this ability.
It does not depend on ActiveRecord like seed_fu and other seeding gems do. So it will work just fine with neo4jrb.
The process is rather simple.
put seedbank in your Gemfile and bundle install.
create directory db/seeds/
create a seeds file under the db/seeds/ directory
for example
#db/seeds/show.seeds.rb
Show.create(name: "Better Call Saul", producer: "Vince Gilligan")
run rake db:seed:show

How to run a rake task by command line in rails

I have defined a rake task as follows in a file called file_locker_task.rake
namespace :myspace do
task :process => :environment do
FileLocker.lock_files
end
end
How do I execute this rake task from the command line?
I tried:
rake myspace:process and rake process but both are throwing an error like this:
rake aborted!
Don't know how to build task 'process'
Run rake -T -A from your Rails home directory to see all the tasks that rake knows about. Yours must be in that list for rake to run it.
By default, in a Rails app, rake looks in the lib/tasks directory and its subdirectories for your .rake files. Check that. (I suspect this is the problem.)
According to docs
Any ruby file (including other rakefiles) can be included with a standard Ruby require command.
-
Additional rake files (with the file extension “.rake”) may be placed in rakelib directory located at the top level of a project (i.e. the same directory that contains the main Rakefile). Also, rails projects may include additional rake files in the lib/tasks directory.

Why is my minitest.rake running twice when I enter rake into terminal?

I created a minitest.rake, as per ryan bates railscast (http://railscasts.com/episodes/327-minitest-with-rails).
When I run rake in the terminal, the test runs, and then runs again before resetting the command line.
require "rake/testtask"
Rake::TestTask.new(:test => "db:test:prepare") do |t|
t.libs << "test"
t.pattern = "test/**/*_test.rb"
end
task default: :test
I'd suppose you already have a task with such a name defined. If you define a new task with the same name it is appended to already existing one.
What if you remove or comment out this code and do rake -T, will test task be there?
Just rename your rake file with a unique name; ex. mintest1.rake.
I had the same issue when I named my rake file paperclip.rake. What ever I defined in that rake file, it got executed twice. I assume this is because the Paperclip gem already defines a rake file paperclip.rake. So I just renamed my paperclip.rake file to pc.rake and it worked for me.
Have you tried rake test:single TEST=path/to/test.rb?
If I were to drop :single then it would run twice.

How do I run a ruby script, that I put in my /lib/tasks/ directory in my Rails app, once?

Eventually I would like to get to setting it up as a Rake task and do a cron job, but for right now...all I want to do is take my ruby script that used to work as a standalone script and have it work within my Rails app.
I renamed the file to be .rake instead of .rb and tried doing rake my_script at the command-line, but that gave me this error message:
rake aborted!
Don't know how to build task 'my_script'
(See full trace by running task with --trace)
How do I run this script within my Rails environment?
This is the first time I am doing something like this, so any assistance would be greatly appreciated.
Thanks.
I think what you're looking for is rails runner. I know in Rails 2.3.x you'd do
ruby script/runner <your file>
In Rails 3 it might be slightly different.
http://guides.rubyonrails.org/command_line.html#rails-runner
The primary difference between a runner and a rake task is : runner would boot rails while rake task doesn't (you can tell it to do so).
Since rake can do both (boot/no boot), there's no concept of runner in rails-3 anymore.
So, create a rake task: whatever_name.rake
Example:
desc "This task does awesome stuff"
task :do_awesome_stuff do
awesome_method
end
def awesome_method
#put your ruby code here
end
Now from your command prompt, type rake do_awesome_stuff to execute this rake task.
To make it boot Rails, change task definition to this:
task :do_awesome_stuff => :environment do

Resources