Rails 8 Adds A New Script Folder By Default And Scripts Generator.

In Rails there is no official way to organize one-off scripts. Unlike recurring tasks, which are automated or scheduled to run at regular intervals (e.g., cron jobs or background workers), these scripts are standalone, ad-hoc scripts that are intended to be run only once or on rare occasions.

Before

Before Rails 8, managing one-off or custom scripts was often chaotic because there was no dedicated folder for scripts that didn’t fit standard Rake tasks or background jobs. This lack of organization made it harder to keep track of these scripts.

As a result, we had to create our own ad-hoc methods for storing and running these scripts. This lead to inconsistent and cluttered solutions.

  • Sometime developers use the bin/ folder but it is primarily for system-level scripts and placing project-specific scripts here often led to confusion.

  • Developers would create custom folders like scripts/, utils/, or tasks/ to store these custom files. While this worked, it caused inconsistency across different Rails projects.

  • Many developers used Rake tasks for custom utilities. While Rake is effective for repeatable tasks, it is not ideal for scripts meant to be run infrequently.

While Rails provided flexible ways to manage custom scripts, it wasn’t always clear where these scripts should be placed.

As a result, custom solutions became the norm, often making it difficult for developers to find, maintain, or execute these scripts efficiently across teams.

After

Rails 8 adds a dedicated script/ folder to applications by default and a scripts generator to hold one-off or general purpose scripts, such as data migration scripts, cleanup scripts, etc.

The new script generator allows us to create such scripts:

rails generate script my_script

We can also specify a folder, when generating scripts:

rails generate script cleanup/my_script

We can then run the generated scripts using:

ruby script/my_script.rb

The new script folder provides a dedicated space for the one-off scripts, allowing developers to maintain a clear and organized project structure.

Need help on your Ruby on Rails or React project?

Join Our Newsletter