Home>

In rails, when you create a rails project, by default (on the README) you will find a Rakefile.
I think that the Rake file refers to a file that personally executes some program such as badge processing, and rails g task ●● will generate a rake file under lib/tasks.
I don't understand the difference between the above two files. Basically, when you create a program that runs regularly, I think that this task is generated, but then I do not understand the significance of Rakefile. I understand that i am doing something for config/application.rb. . .

  • Answer # 1

    Rakefile is a file thatrakecommand reads automatically.
    Reference: https://docs.ruby-lang.org/en/latest/library/rake.html

    Rails also uses rake to define development tasks, so the first code loaded isRakefile.
    However, since the Rakefile generated by Rails contains code that calls the process of reading the rake file under the lib/tasks directory from the beginning, the task definition file placed in lib/tasks/Seems to be loaded without the programmer doing anything.
    Reference: https://qiita.com/tbpgr/items/5c2f192da0ccf8fad5e1

    So

    Rakefile

    rakecommand reads files by default

    lib/tasks/**/*. rake

    Files read by Rails conventions

    I think it is a difference of

    .
    You should write tasks in the latter while using Rails, and in the former when using outside Rails.


    Aside

    By the way

      

    I think that a Rake file is a file that personally executes some program such as badge processing

    Rake itself is one of Ruby's standard libraries, just a kind of task runner.
    It just exists to run Ruby processing in order considering dependencies.

    Since it is

    , it is very convenient to use it for Batch processing etc., but it is not dedicated to it.
    Rails is also used to provide auxiliary commands.