← Home
rails

Rails Generate task

Reference & Command Builder

Published: 2023-05-11
Author: Harrison Broadbent
Type: Reference & Command Builder

Rails Generate Task

Rails Generate Task (rails g task) is the generator for Rake tasks. Rake tasks define CLI commands for common administration task; db:migrate and assets:precompile are two Rake tasks that most Rails developers are familiar with.

When you generate a new Rake task with rails g task, a file inside lib/tasks will be created with a task skeleton; It will look something like this —

# shell
❯ rails g task app update
 
# lib/tasks/app.rake
namespace :app do
  desc "App related tasks"
 
  task :update do; end
end

To run the update task from above, you would use rake app:update.

To see a full list of tasks, you can use rake --tasks or rails --tasks. In general, you can use rails as an alias for rake when running tasks via the command line.

Command Builder for rails g task

rails g task 

Task name

Task actions

Generator Options

Runtime Options

# Command Options
❯ rails g task

Usage:
  rails generate task NAME [action action] [options]

Options:
  [--skip-namespace], [--no-skip-namespace]              # Skip namespace (affects only isolated engines)
  [--skip-collision-check], [--no-skip-collision-check]  # Skip collision check

Runtime options:
  -f, [--force]                    # Overwrite files that already exist
  -p, [--pretend], [--no-pretend]  # Run but do not make any changes
  -q, [--quiet], [--no-quiet]      # Suppress status output
  -s, [--skip], [--no-skip]        # Skip files that already exist

# Command Description
Description:
    Generates a new Rake task. Pass the namespace name, and a list of tasks as arguments.

    This generates a task file in lib/tasks.

Example:
    `bin/rails generate task feeds fetch erase add`

        Task:      lib/tasks/feeds.rake