[ rake ] writing own rake task

rake = ruby make

$ rake db:schema:dump
$ rake db:schema:dump RAILS_ENV=production

 

RakeFile : ruby generated

# 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__)

Rails.application.load_tasks

 

List rake tasks

$ rake -T

 

The task will be in lib/tasks

lib/tasks/app/ns1.rake

namespace :app do

namespace :ns1 do
desc "TODO"
task :my_task_name1 => :environment do
end

desc "TODO"
task :my_task_name2, [:var1, :var2 ]=> :environment do

var1 = args[:var1] || 1.month.ago.beginning_of_day
end
end

end 

NOTE : :environment parameter Your whole environment by making tasks dependent on the environment task. This lets you do things like run rake RAILS_ENV=staging db:migrate

ref : https://stackoverflow.com/questions/7044714/whats-the-environment-task-in-rake

Execute

rake app:ns1:my_task_name1
Advertisements

One thought on “[ rake ] writing own rake task

  1. Pingback: [ rails ] delayed_job | dsin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s