[RUBY] Let's define a Rake task.

What is a Rake task

What does a task mean, "to do"? When I first heard it, I thought so, but when I looked it up, it seems that there are tasks in Ruby on Rails.

What is a Rake task?

This task function seems to be able to execute arbitrary processing from the terminal without launching the application one by one. The application I'm working on needed sample data during the development stage, so I created it using this task.

Try to create a task

rails g task sample

This will create a file for the task sample.rake. Create a file named sample.task in lib / tasks.

namespace :greet do
end

By default, the above code is written in the created file. In this, write the process you want to perform as a task so that it can be executed with a single command.

#Write the task name.
namespace :sample do
  #Write a description of the task. desc=>description
  desc ""
  # db =>Write the name of the task.
  task db: :environment do
    #A place to describe the process you want to execute
  end
end

From above,,,, ● Write your name → namespace ● Task description → desc ● Task name → task db: In the above example, db is so.

namespace :sample do
  desc "Task to display as a gorilla"
  task gorira: :environment do
    puts "gorilla"
  end
end

Check if the created task works properly.

rails sample:gorira

gorilla

This completes the rake task.

Recommended Posts

Let's define a Rake task.
Rails: How to write a rake task nicely
[Rails] Implement rake task
rails learning rake task, cron, whenever
[Rails] Status update using Rake task
Define a task to ZIP archive a set of project files in Gradle