Hi Friends,
In my previous blog Rails Command Line Tools. I discussed about some rails command line tools. Today I am going to tell you about rake command in rails. rake is actually a ruby version of unix's make command. It is also used to performs some actions on your rails application. It is dependent on the directory in which you currently are. There are so many pre-build tasks for rails application. You can list the available rake tasks on a directory by hitting this command on your directory.
rake --tasks:
=> Output will be like this that will also contain the description of each task
$ bin/rake --tasks
rake about # List versions of all Rails frameworks and the environment
rake assets:clean # Remove old compiled assets
rake assets:clobber # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake db:create # Create the database from config/database.yml for the current Rails.env
...
rake log:clear # Truncates all *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)
rake middleware # Prints out your Rack middleware stack
...
rake tmp:clear # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)
rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids
Rake commands are used for several purposes. Some of them are:
1. For database related operations:
Some of the operations that needs to be performed on db like running migration to simulate the tables as migration suggests
rake db:create
## To create db for the application
rake db:setup
## It not only creates the db but also runs the migration
rake db:migrate
## It runs the latest migrations and simulates the db
rake db:rollback
## It rollbacks the previous migration and sets the db to the previous state
2. Assets related tasks:
For compiling the assets (js, css) and cleaning them
rake assets:precompile
rake assets:clean
3. about tasks
It gives information about your rails application
$ rake about
About your application's environment
Rails version 4.2.5.2
Ruby version 2.2.1-p85 (x86_64-linux)
RubyGems version 2.4.8
Rack version 1.6.4
JavaScript Runtime Node.js (V8)
Middleware Rack::Sendfile, ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000002fd2558>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, WebConsole::Middleware, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Remotipart::Middleware, Rack::Head, Rack::ConditionalGet, Rack::ETag, Warden::Manager, OmniAuth::Builder, Rack::Pjax
Application root /home/shwetasingh/rails_apps/article_app_1
Environment development
Database adapter postgresql
Database schema version 20160428123153
4. Creating custom rake tasks:
For creating custom rake tasks you can hit this command that will create a file with the namespace provided
rails g task test_task
## =>
create lib/tasks/test_task.rake
Now the generated file will look like this:
namespace :test_task do
## Write your tasks here
end
For more information related to custom rake task you can go through this link
http://findnerd.com/list/view/Create-a-Custom-Rake-Task/20576/
Hope you liked this blog. Will talk about more rails things soon.
0 Comment(s)