1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Railsのrakeタスク

Last updated at Posted at 2022-06-23

定義されているタスクの確認

% bundle exec rake -T

rakeタスクの作成

ファイルの作成

% rails g task :namespace :task_name

lin/tasks/task_name.rbが作成される。

作成されたファイル

task_name.rb
namespace :namespace do
 desc "TODO"
 task task_name: :environment do
 end
end

実行したい処理を記述

task_name.rb
namespace :task_name do
 desc 'あいさつをする'
 task task_name: :environment do
  puts 'おはようございます'
 end
end

descには処理内容の説明を記載する。
:environmentはRailsで準備されているenvironmentタスクを先に実行している。

rakeタスクの実行

% bundle exec rake namespace:task_name
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?