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.
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.
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.