Search code examples
ruby-on-railsrake

Why are rake tasks stored in lib/tasks/?


My understanding of the lib/ directory in rails is that it stores non-domain specific code as a best practice.

However, my Rake scripts are very specific to my domain. They do things like create new models.

So is there a better place than lib/tasks/ to store domain-specific rake scripts, or am I missing something here?


Solution

  • I like this idea, and I agree - lib at one point was very much a junk drawer, and as a Rails community we've moved some of the junk away, but yes Rake tasks are usually very specific application logic.

    In your Rakefile all you have to do is load your new Rakefiles (exercise for the reader: iterate the files in the folder instead of specifying it explicitly.

    Example:

    require File.expand_path('../config/application', __FILE__)
    
    Rails.application.load_tasks
    
    load('app/tasks/my_task.rake') # <--- my custom task!!!