LoginSignup
4
3

More than 3 years have passed since last update.

Rakeタスクの実装

Last updated at Posted at 2019-07-27

本日の学習内容のアウトプット。

Rakeタスクとは、ターミナルなどのコマンドライン上からアプリケーションを実行できる機能の一つです。
CSVデータのインポートなどがRailsコンソールなどを使用しなくても簡単に処理ができるようになります。

Rakeタスクの実装

Rakeタスクの実装は以下のコマンドを入力します。

rails new rake タスク名

すると、lib/tasks/task_sample.rakeが生成されるのでこのファイルに記述して行きます。

namespace :import_csv do
  # rake import_csv:users
  desc "User CSVデータのインポート #実行処理の説明"
  task users: :environment do
    User.import('db/csv_data/user_data.csv')
  end
end

Railsでは、デフォルトで設定されているRakeタスクがあります。
これらのタスクは、アプリを作成する際自動で定義されます。
以下のコマンドをターミナルに入力すると、先ほど作成したRakeタスクと一緒に表示されます。

rake -T

表示されたRakeタスク

user-no-MacBook-Pro:csv_data user$ rake -T
rake about                              # List versions of all Rails framew...
rake active_storage:install             # Copy over the migration needed to...
rake app:template                       # Applies the template supplied by ...
rake app:update                         # Update configs and some other ini...
rake assets:clean[keep]                 # Remove old compiled assets
rake assets:clobber                     # Remove compiled assets
rake assets:environment                 # Load asset compile environment
rake assets:precompile                  # Compile all the assets named in c...
rake cache_digests:dependencies         # Lookup first-level dependencies f...
rake cache_digests:nested_dependencies  # Lookup nested dependencies for TE...
rake db:create                          # Creates the database from DATABAS...
rake db:drop                            # Drops the database from DATABASE_...
rake db:environment:set                 # Set the environment value for the...
rake db:fixtures:load                   # Loads fixtures into the current e...
rake db:migrate                         # Migrate the database (options: VE...
rake db:migrate:status                  # Display status of migrations
rake db:rollback                        # Rolls the schema back to the prev...
rake db:schema:cache:clear              # Clears a db/schema_cache.yml file
rake db:schema:cache:dump               # Creates a db/schema_cache.yml file
rake db:schema:dump                     # Creates a db/schema.rb file that ...
rake db:schema:load                     # Loads a schema.rb file into the d...
rake db:seed                            # Loads the seed data from db/seeds.rb
rake db:setup                           # Creates the database, loads the s...
rake db:structure:dump                  # Dumps the database structure to d...
rake db:structure:load                  # Recreates the databases from the ...
rake db:version                         # Retrieves the current schema vers...
rake dev:cache                          # Toggle development mode caching o...
rake import_csv:users                   # User CSVデータのインポート
rake initializers                       # Print out all defined initializer...
rake log:clear                          # Truncates all/specified *.log fil...
rake middleware                         # Prints out your Rack middleware s...
rake notes                              # Enumerate all annotations (use no...
rake notes:custom                       # Enumerate a custom annotation, sp...
rake restart                            # Restart app by touching tmp/resta...
rake routes                             # Print out all defined routes in m...
rake secret                             # Generate a cryptographically secu...
rake stats                              # Report code statistics (KLOCs, et...
rake test                               # Runs all tests in test folder exc...
rake test:db                            # Run tests quickly, but also reset db
rake test:system                        # Run system tests only
rake time:zones[country_or_offset]      # List all time zones, list by two-...
rake tmp:clear                          # Clear cache, socket and screensho...
rake tmp:create                         # Creates tmp directories for cache...
rake yarn:install                       # Install all JavaScript dependenci...

真ん中あたりに作成されたRakeタスクがあります。

コマンドの実行

user-no-MacBook-Pro:csv_data user$ rake import_csv:users

Rakeタスクは非常に便利な機能だと思ったのでもっと使ってみて学習していきたいです。

参考
https://qiita.com/yoshito410kam/items/26c3c6e519d4990ed739
https://qiita.com/suzuki_koya/items/787b5562d2ae1a215d94

4
3
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
4
3