Help installing delayed_job - ruby-on-rails

I'm trying to use collectiveidea's delayed_job gem
The installation instructions include
Rake tasks are not automatically loaded from gems, so you’ll need to add
the following to your Rakefile:
begin
require 'delayed/tasks'
rescue LoadError
STDERR.puts "Run `rake gems:install` to install delayed_job"
end
Where is my Rakefile? And what is a Rakefile?

I had the same problem with rails 3.1 and collectiveidea-delayed_job.
Once I added Delayed::Worker.backend = :active_record in the initializer I got the error
no such file to load -- delayed/backend/active_record (LoadError)
The solution for me was to add gem 'delayed_job_active_record' in the gemfile, as suggested here

I have the same problem and put that code in delayed_job.rake in the lib/tasks directory. It works, but now It say's:
*** Starting job worker localhost pid:79949
rake aborted!
uninitialized constant Delayed::Job
What is wrong now?
UPDATE: I just got a mail answer from Brandon:
Theres a bug in the latest version where it doesn't get properly initialized when using the rake task. If you create a file in config/initializers and put the follow in it, the error should go away:
Delayed::Worker.backend = :active_record

The Rakefile is a file that is used to configure rake, a Ruby build tool (sort of like make, but all in Ruby). In a Rails project, there is a file in the top project directory named Rakefile where you can insert this code.
Alternatively, you can add a file into the lib/tasks directory (for example named delayed_job.rake) and put the code into there. The name of the file is not important as long as
It is in the lib/tasks directory
It has the extension .rake

Related

What can be done to fix the following error when we run any rails command: " `require_relative': cannot load such file "

Any rails command doesn't work for me. I have several versions of ruby installed through rvm. I tried installing rails with all the versions, they do install successfully but with all of them I face the following error whenever I run any rails command in my project directory:
~ rails new blog
Traceback (most recent call last):
1: from bin/rails:3:in `<main>'
bin/rails:3:in `require_relative': cannot load such file -- /Users/Am33d/Documents/config/boot (LoadError)
I tried looking up for the error but didn't find any solutions.
What can be done to fix this? I am using macOS Mojave (10.14.6)
This error would indicate that you do not have a boot.rb file in your config directory for some reason. When running rails commands -- regardless of if you run them as bin/rails [command] or bundle exec rails [command], runs the bin/rails file. This file typically has a line require_relative '../config/boot. The boilerplate bin/rails file in a new Rails 6 app is:
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
To simply fix this you can create a blank file by running touch config/boot.rb from the root directory of your application and that would itself suppress the error. Having said that, you'd be better off creating a config/boot.rb file that contains useful information. The boilerplate config/boot.rb file is this
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
Without this file you are not necessarily appropriately loading the gems from your gemfile or caching operations with bootsnap.
When I ran into this problem, I also received the same error when trying to use rails -s.
If the same is happening for you, its because your version of ruby isn't compatible with your rails version.
After upgrading to the latest rails version the error stopped and everything worked well.
A little bit weird.
rails new blog should not need to find boot file, neither need bundler. Actually, you are trying to create a new project, so it is expected there is not Gemfile, boot, or bundler created for your project.
So I would advise you to find what rails command is being executed. Sometimes one rails executable created by bundler is taking precedence in the $PATH environment variable.
Ex: some incorrect bin/rails executable is on your $HOME directory.
i was trying to build an app using 'rails server' and this error was showing. You have to make sure that the rails version matches the ruby version and even if you do what an answer above said (create the config/boot.rb paste) doesnt work, than you have to change the version of rails to the stable. I'll let the link of this problem here, there's an issue closed in github for this problem https://github.com/rails/rails/pull/43951
to solve the problem you have to replace the gem rails line on the gemfile to this:
gem "rails", github: "rails/rails", branch: "7-0-stable"
Sorry about my english.

Why is this gem not adding rake tasks to a Rails app?

We have a gem which runs via a Rake task. The task is defined in lib/tasks/<namespace>.rake. After reading Rake tasks inside gem not being found I confirmed that the .gemspec includes the file defining the task; there is also a railtie which should be including the tasks as suggested in including rake tasks in gems. And yet our Rails 4.1 application doesn't seem to load the Rake task.
What am I missing?
I just successfully tested your gem and I can see no problem with the rake tasks in it:
I added gem_fresh to the Gemfile and ran bundle, the gem installed
Immediately I could see the rake present in the list of rakes:
$ rake -T outdated
rake gem_fresh:outdated # outdated
Then I updated the railtie.rb file to use the load method to load the rake defined in lib/task/metrics.rake and searched the available rakes again:
# lib/gem_fresh/railtie.rb:
rake_tasks do
namespace :gem_fresh do
desc "outdated"
task :outdated => :environment do
GemFresh::Reporter.new.report
end
end
load "tasks/metrics.rake"
end
$ rake -T outdated
rake gem_fresh:outdated # outdated
rake metrics:outdated_gems # display outdated gem version metrics
So, I can see no problem with your gem. Both methods in railtie (inline rake as well as using the load method) seem to work OK. The only difference I noticed is that I tested this on rails 4.2 but I rather doubt that would make a difference.
Did you put the gem into your Gemfile? If I remove it from there, I indeed see no gem_fresh rakes defined.
The problem was not with the gem, but with the way it was included in the app.
In the Gemfile, this works and includes the rake task:
gem 'gem_fresh'
This works but doesn't include the rake task:
group :development do
gem 'gem_fresh'
end
This seems to be due to how Bundler requires gems in Rails apps. From the documentation:
By default, a Rails generated app calls Bundler.require(:default, Rails.env) in your application.rb, which links the groups in your Gemfile to the Rails environment.
If for some reason that Rails.env argument wasn't evaluating to include the :development group, which seems to be the case when I call rake -T, the gem wouldn't be Bundler.require-d, and the rake tasks wouldn't be loaded.
The fact that it isn't including the :development group seems to be an odd Bundler "gotcha", but at least now I know that moving it to the default group solves the issue and it's not a problem with the gem.
in my case, the project requiring the gem had a rake file, and the gem I was requiring had the same name when I changed the name of the rake file I could see the tasks in the project.
my_gem had lib/tasks/XXXX.rake and my_proj also had lib/tasks/XXXX.rake,
after I changed my_gem XXXX.rake to YYYY.rake I managed to list and use the tasks in YYYY.rake

Rails Rspec `require': cannot load such file -- rails_helper (LoadError)

I am trying to use Rspec for my test. When I run
$ rspec mytest_spec.rb
I get the following error due to the
/home/bastien/.merbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- rails_helper (LoadError)
I have tried many things and somehow if I run
rspec spec
or
bundle exec rspec
from the folder where my .rspec file is I do not get any error. I have created an app just to dry test this issue (I created a new rail app, added rspec in my Gemfile, run the bundle install command and the rspec:install command, generated a scaffold and run the tests. Could anyone explain to me why I get this issue and how I can get rid of it? Do I do something wrong when I try to run only one single spec? Thanks.
You are getting that error because you're trying to call your spec like this...
rspec mytest_spec.rb
You need to call it like this from your app's root folder , not inside the spec folder. So first get in the right folder
cd ~/
cd path_to_your_rails_app
Then call your spec
rspec spec/the_rest_of_the_path_to_your_spec/mytest_spec.rb
for instance
rspec spec/models/mytest_spec.rb
For others who find this:
I got this error, 'require': cannot load such file -- rails_helper (LoadError), when I had included the rspec-rails gem but hadn't run rails generate rspec:install which generates the spec/rails_helper.rb file. So if the other solution doesn't help you, make sure you've done that.
For those that the answers above didn't help:
You just need to add gem 'rexml' to your Gemfile.rb, run bundle install and try again.

Where do I put custom capistrano recipes?

I'm trying to manage database.yml using capistrano, following this post:
http://www.simonecarletti.com/blog/2009/06/capistrano-and-database-yml/
I'm running into trouble including the code used in the post above. I've named this file cap_database.rb but I don't where to save it, or how to load it in deploy.rb.
I've tried placing it in lib/capistrano and added it to deploy.rb with this line:
require 'capistrano/cap_database'
and then I get this:
$ cap deploy:db
/home/daniel/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- capistrano/cap_database (LoadError)
Why can't it find the file?
it's just a shot but try
bundle exec cap deploy:db
For recipes at the gem level, put your custom recipes into GEMDIR/CAPGEMDIR/lib/capistrano/recipes
If you want to include additional files into your app's deploy.rb, then you can do it using this method:
require in capistrano deploy.rb cannot find file
I know this is a bit late but here goes anyway.
If you store cap_database.rb in config/recipes, it can be included in deploy.rb using load config/recipes/cap_database.

How to move your rake tasks to the rails lib dir from in a gem generator?

Im creating a custom gem and have some rake files that need to be installed into the users /lib/tasks directory to work. I want create a generator that will move / create my rake tasks in the tasks directory so they can use them.
Does anyone have any idea how do to this in a custom gem?
Ensure that your gemspec includes the tasks
and then in your Rakefile
require 'bundler'
Bundler::GemHelper.install_tasks
You just need require your rake file in your Rakefile of your root project.

Resources