rails aborted! Don't know how to build task 'task_name' - ruby-on-rails

I was creating a quick one-off task:
require 'yaml'
task generate_permissions_yaml: :environment do
permissions = []
Permission.order(:title).each do |permission|
permissions << {
title: permission.title,
code: permission.code,
description: permission.description
}
end
puts permissions.to_yaml
end
When I ran it with rails generate_permissions_yaml, I got this error:
rails aborted!
Don't know how to build task 'generate_permissions_yaml' (see --tasks)
/bundle/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
/bundle/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/bundle/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/bundle/gems/railties-5.0.2/lib/rails/commands.rb:18:in `<top (required)>'

As it turns out, it was due to the way that I named the file. It should have a .rake extension, not .rb.
I renamed this:
lib/tasks/generate_permissions_yaml.rb
To this:
lib/tasks/generate_permissions_yaml.rake
I uncovered this after trying to run rails g task generate_permissions_yaml and seeing that it generated a file with a .rake extension.

For newbies - this is a common error if you use a space instead of a colon in a rake command. For example if you enter:
rake db migrate
instead of:
rake db:migrate
The correct format has a colon, not a space

Related

"No such file" error when using `gets.chomp` in seeds.rb

I wanted my seeds.rb file to have two paths based on some user input. For the sake of simplicity in this question I've stripped it down to just these two lines:
print "> "
res = gets.chomp
When I run rake db:seed, the following exception is raised:
▶ rake db:seed
> rake aborted!
Errno::ENOENT: No such file or directory # rb_sysopen - db:seed
/home/me/work/my_app/db/seeds.rb:5:in `gets'
/home/me/work/my_app/db/seeds.rb:5:in `gets'
/home/me/work/my_app/db/seeds.rb:5:in `<top (required)>'
Anyone know why this is happening, i.e. why gets.chomp in this context is causing the program to try to open a file named db:seed?
Try using STDIN.gets.chomp instead of gets.chomp.
See What's the difference between gets.chomp() vs. STDIN.gets.chomp()? for the explanation.

What's a Capfile? And how do I use it correctly with Ruby on Rails?

I have a ruby on rails application that should be transferred from one server to another (that's not working fine at the moment). I installed capistrano. I'm using debian 7 server with apache passenger.
I saw a Capfile in the root directory of the ruby on rails application.
Is a Capfile something like a makefile to build and deploy the application? How do I correctly work with it?
UPDATE
When I run the cap file i got this: ' cap deploy:setup '
(Backtrace restricted to imported tasks)
cap aborted!
LoadError: cannot load such file -- config/deploy
/var/www/myapplication/Capfile:3:in `load'
/var/www/myapplication/Capfile:3:in `<top (required)>'
(See full trace by running task with --trace)
it seems like the deploy file coun't be loaded ... i have in the config folder an deploy folder and a deploy.rb
whats going wrong?
This is happening because Capistrano tasks are now Rake tasks and Rake expects a construct like this:
task :name, [:arg, :arg] => :dependency do
...
end
Capistrano 3:
task :dump do
on primary roles :db do
...
end
end

How do I get a custom Rake task to run in Sinatra?

*I want to get a custom Rake task to run in my Sinatra app but I keep getting rake aborted!
Don't know how to build task 'greet'.
Here's the custom Rake task (greet.rake) for testing purpose:
task :greet do
puts "Hello!"
end
I've put the greet.rake in ./lib/tasks (Rails). I'm guessing Rake can't find the correct directory for the file.
How do I get a custom Rake task to run in Sinatra?
I'm using Ruby 2.0.0 and Sinatra 1.4.4.
UPDATE
The Rakefile now looks like this:
require "./app"
require "sinatra/activerecord/rake"
require "./lib/tasks"
When using:
rake greet
I get:
rake aborted!
cannot load such file -- ./lib/tasks
/Users/*/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport- 4.0.1/lib/active_support/dependencies.rb:229:in `block in require'
/Users/*/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport- 4.0.1/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/*/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:229:in `require'
/Users/*/Dropbox/Development/Sinatra/sinatra-mp-experiment/Rakefile:3:in `<top (required)>'
(See full trace by running task with --trace)
Create a Rakefile at your Sinatra app's top directory, require the file that contains this task you want to use and you should be good to go.
Edit:
A simple solution is changing your Rakefile to:
require "./app"
require "sinatra/activerecord/rake"
Dir.glob('lib/tasks/*.rake').each { |r| load r}
Now any .rake file under lib/tasks will be loaded.

How do I call another task in Rake

Although this might sound similar to the other questions you find here, there is a slight twist. I have two directories, say /home/rails/Rake and /home/rails/test_app. The rails directory is where I place all my rails projects.
Inside Rake, I have a Rakefile and a create.rake file.
This is what my rakefile look's like
namespace :setup do
desc "something"
task :init do
print "Name of the destination directory: "
name = STDIN.gets.strip
cp_r '.', "../#{name}/lib/tasks"
cd "../#{name}"
sh "rake setup:create"
end
end
And create.rake
namespace :setup do
desc "Install"
task :create do
sh 'git init'
#some other code
end
end
What it does is obvious. I want to copy the contents of the Rake directory to /test_app/lib/tasks. Then change directory to test_app and run setup:create task defined in the install.rake file now placed in test_app/lib/tasks. This works, but is this the rake way of doing it? Can anyone give me a slight hint of how it's done, the Rake way.
Here is the error which I get when I used invoke method:
$ rake setup:init
Name of the destination directory:
testapp
cp -r . ../testapp/lib/tasks
cd ../testapp
rake aborted!
Don't know how to build task 'setup:create'
/home/TradeRaider/rails/Rake/Rakefile:8:in `block (2 levels) in <top (required)>'
/home/TradeRaider/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/home/TradeRaider/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => setup:init
(See full trace by running task with --trace)
This is more rake-ish :)
Rake::Task["setup:create"].invoke
Although #apneadiving answer helped, it just struck me that I was trying to call a Rakefile from another Rakefile, literally speaking. Anyways, to do so, I had to first load the rake file,
load "../#{name}/lib/tasks/create.rake"
(requiring it will also do the trick)
and then invoke it.
Rake::Task["setup:create"].invoke

Rails 3 rake task can't find model in production

My simple rake task, stored in lib/tasks/items_spider.rake runs just fine in development. All it does is call spider! on the Item model.
namespace :items do
desc "Spider the web for data, hoorah"
task :spider => :environment do
Item.spider!
end
end
I have the :environment task as a dependency, so everything works just fine. However, when I add RAILS_ENV=production, I hit errors, both on my local server and the production server:
$ rake items:spider RAILS_ENV=production --trace
(in /home/matchu/Websites/my-rails-app)
** Invoke items:spider (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute items:spider
rake aborted!
uninitialized constant Object::Item
/home/matchu/.rvm/gems/ruby-1.9.2-preview3#rails3/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing'
/home/matchu/.rvm/gems/ruby-1.9.2-preview3#rails3/gems/rspec-core-2.0.0.beta.22/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'
/home/matchu/.rvm/gems/ruby-1.9.2-preview3#rails3/gems/rspec-expectations-2.0.0.beta.22/lib/rspec/expectations/backward_compatibility.rb:6:in `const_missing'
/home/matchu/Websites/openneo-impress-items/lib/tasks/items_spider.rake:4:in `block (2 levels) in <top (required)>'
/home/matchu/.rvm/gems/ruby-1.9.2-preview3#rails3/gems/rake-0.8.7/lib/rake.rb:636:in `call'
[...trace of how rake gets to my task...]
This just seems odd to me. Apparently the models have not been loaded correctly. I'm on Rails 3.0.3, though development on this app started back when Rails 3 was in beta. How can I go about debugging this issue? Thanks!
Contrary to running your application in production, a Rake task does not eager load your entire code base. You can see it in the source:
module Rails
class Application
module Finisher
# ...
initializer :eager_load! do
if config.cache_classes && !$rails_rake_task
ActiveSupport.run_load_hooks(:before_eager_load, self)
eager_load!
end
end
# ...
end
end
end
So only if $rails_rake_task is false, will the application be eager-loaded in production. And $rails_rake_task is set to true in the :environment Rake task.
The easiest workaround is to simply require the model that you need. However, if you really need all of your application to be loaded in the Rake task, it is quite simple to load it:
Rails.application.eager_load!
The reason all of this work in development is because Rails autoloads your models in development mode. This also works from within a Rake task.
In your environment/production.rb, you should add the following:
config.dependency_loading = true if $rails_rake_task
It solved the problem for me.
(Note: this should be added AFTER the config.threadsafe! call)
The same thing happens to me in Rails 6.0.3.2.
I was trying to configure the rake file task :foo do ... end. Instead it should've been task foo: :environment do ... end.
Just found another one: I was developing on windows, deploying to Heroku. The web app and rails console worked fine, but rake tasks and even direct require could not load the model. Turned out I had absentmindedly created the model file as Model.rb instead of model.rb - system dependent case sensitivity.

Resources