What does "rake routes" actually do? - ruby-on-rails

In a fresh(ish) rails project, the Rakefile looks like this:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
Blog::Application.load_tasks
Yet rake routes produces the following output:
cpe-74-72-73-47:rails-blog-example djechlin$ rake routes
home_index GET /home/index(.:format) home#index
root / home#index
I don't understand how rake works so that it can get to the routes file or the routes task. Per the command line usage documentation, rake is invoked as
rake [options ...] [VAR=VALUE ...] [targets ...]
But the page has no explanation of what the targets are. I assume rake is called directly on the routes.rb file from this and that Rakefile is not related, but I can't confirm this at all.

A Rakefile contains executable Ruby code. Anything legal in a ruby script is allowed in a Rakefile.
When you trigger rake routes you call this piece of Ruby code .

Actually, the Rakefile is very related, and rake isn't called directly on routes.rb at all. Rake needs a rakefile. The magic happens inside load_tasks, which load the numerious Rails-specific Rake tasks that come with the framework.
When you invoke Rake, it looks for a Rakefile. The Rakefile is just Ruby. In your default Rakefile, first it includes ../config/application, where your application class (Blog::Application) is defined; then it invokes load_tasks, which is provided by Rails::Application, from which your Blog::Application inherits.
From there there are a million ways for each part of Rails to make Rake tasks available. Typically the core libraries provide Rail ties which expose tasks.

Related

Where is the Rakefile in a Railsapp?

As the question states, where is the Rakefile in a Railsapp?
In my app it is no where to be found. I'm working on an app with another developer and he's just pushed code to the Rakefile as such:
7  Rakefile
## -0,0 +1,7 ##
+# Add your own tasks in files placed in lib/tasks ending in .rake,
+# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
+
+require File.expand_path('../config/application', __FILE__)
+require 'rake'
+
+Newapp::Application.load_tasks
Although, I can't find this code anywhere in the actual app. Please help clear this up for me!
Thanks!
Conventionally, the Rakefile usually goes at the Rails root of your project (this is the location first generated when you ran rails new ...).
rake can also be run by using a Rakefile anywhere, by using rake -f path/to/my/rakefile.rb, so if you need to diverge from the convention for some reason, that's possible too.
However, note that many third-party services presume that it's possible to run rake from the root of your project and get the desired result (e.g. heroku rake).

Rake aborted, no rake file found

Hey people i know maybe 1% of ruby and need many help...
I migrated a system from one server to another server
When I try to insert an entry to the database using the system, I get the following error (remembering that I use ruby is 2.1):
undefined method `insert_record' for #<Array:0x007fec2a2b2fa0>
i know maybe this is a solution:
http://alok-anand-ror.blogspot.com.br/2013/11/undefined-method-insertrecord-for.html
i have gems of rake instaled 0.9.2 and 0.9.2.2
when i try run "rake db:migrate" (I think it would solve, I do not know) i received
rake aborted No rake file found
if somebody can help me I greatly appreciate.
thanks in advanced.
it seems your Rakefile is missing .
Check your rails folder should contain Rakefile with following content
#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
Demo::Application.load_tasks
~
and
undefined method `insert_record' for #<Array:0x007fec2a2b2fa0>
this error is because of incompatibility issues with rails and ruby , try upgrading rails

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.

Rails's Rake task takes exceptionally long to execute? [duplicate]

rake --tasks takes about 18s to run. This is just the time it takes to load all the tasks, as a result any task I define will take at least this amount of time to run :
$time rake --tasks
rake db:clean # Cleaning up database
rake passenger:restart # Restart Application
rake spec # Run specs
real 0m18.816s
user 0m7.306s
sys 0m5.665s
My Rakefile :
$: << "."
require "rubygems"
require "rspec/core/rake_task"
desc "Run those specs"
task :spec do
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w{--colour --format progress}
t.pattern = 'spec/*_spec.rb'
end
end
task :default => :spec
Any idea why rake takes to much times ?
Thanks
Try spring
Command line will look like:
spring rake -T
It will take more time running the first time, but subsequent runs will be very fast.
This solution worked for me: Faster rake tasks in Rails.
I had to do a little variation where I created a lib/tasks/no_rails directory and put all the Rake files which do not need Rails in there and loaded only those using the above method.
I like the solution Pratik mentions for the general case of loading rails for tasks that need it and not for those that don't, for any rake task without having to remember beforehand.
A less-invasive method to run a rake task that doesn't need rails is to use the -f rake option to tell rake to use a particular Rakefile. This way, rake won't go looking for rake tasks in all of rails.
For example, assuming your task above is in a file called Rakefile at the top level of your project and your Rakefile doesn't do anything that loads Rails like require File.expand_path('../config/application', __FILE__), you can do:
$ rake -f Rakefile spec
and it should run your spec task much faster. Try $ time rake -f Rakefile -T; I did this with a rails-independent Rakefile of mine and got:
real 0m1.543s
user 0m1.308s
sys 0m0.201s
The downside is you have to remember to specify this option every time, and not to specify it if you want to run a rake task from rails like rake db:migrate.
The entire rails environment has to be loaded, therefore even simple rake tasks such as rake --tasks take a while. Opening a console with rails console or script/console takes a similar time. You may try to hack Ruby or Rails to speed up rake, but too much optimization can be bad if you want to switch to a newer version later. Since the rails environment must be loaded, cleaning up routes may also help.

rake db:create - rake aborted - no rakefile found

So, the title is quite self explanatory, but here's the following ..
rake db:create
rake aborted!
No rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
Any help would be appreciated.
What is your current working directory when calling the rake db:create command? Are you in the root of the Rails app?
You have to be in the root of the Rails app that you are creating.
Currently you must be one step up.
Case I:
Check out your directory. If you are in same directory where your application is available then you wont' get is this message. You are getting this message because you are out of your application directory. To check you present directory you can use this command
pwd
Case II
You might missing your Rakefile. Check it out in your directory. For example,
$ ls
app/
bin/
config/
db/
...
If you don't find Rakefile then create new one. Puts this code
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
Blog::Application.load_tasks

Resources