rails-generate
Generate new Rails application components
TLDR
List all available generators
Generate a new model named Post with attributes title and body
Generate a new controller named Posts with actions index, show, new and create
Generate a new migration that adds a category attribute to an existing model called Post
Generate a scaffold for a model named Post, predefining the attributes title and body
SYNOPSIS
rails generate <GENERATOR> [arguments] [options]
Examples:
rails generate model User name:string email:string
rails generate controller Products index show
rails generate scaffold Post title:string body:text --skip-stylesheets
rails generate migration AddDetailsToProducts description:text part_number:string
PARAMETERS
--help, -h
Display help for the specified generator, or list all available generators if no generator is given.
--pretend, -p
Run in dry-run mode. Shows the files that would be generated without actually creating them.
--force, -f
Overwrite files that already exist during generation.
--skip, -s
Skip files that already exist, preventing them from being overwritten.
--quiet, -q
Suppress status messages during the generation process.
--no-color
Disable colorized output for generator status messages.
--skip-routes
Do not add an entry to the application's config/routes.rb file.
--skip-test, --no-test-framework
Do not generate any test files for the component.
--skip-assets
Do not generate asset files (e.g., JavaScript or CSS) for the component.
DESCRIPTION
The rails generate command is a fundamental tool in the Ruby on Rails framework, used to automate the creation of various application components. It leverages pre-defined templates and Rails' "convention over configuration" philosophy to generate boilerplate code for models, controllers, views, migrations, scaffolds, mailers, jobs, and more.
By providing the generator name and specific arguments (e.g., model name, controller actions, database fields), rails generate quickly sets up the necessary files and directories, often including associated test files, routes, and database migration files. This drastically reduces the amount of manual setup, ensures consistency, and helps developers adhere to Rails best practices. It's an indispensable command for rapidly building and expanding Rails applications, working in conjunction with rails destroy which serves as its counterpart for removing generated code.
CAVEATS
While rails generate is powerful, care must be taken when using the --force option, as it will overwrite existing files without confirmation. Generated code provides a solid starting point but rarely fits all needs perfectly; manual review and customization are always required. Over-reliance without understanding the generated code can sometimes lead to unnecessary boilerplate.
LISTING AVAILABLE GENERATORS
Running rails generate without any arguments will display a comprehensive list of all built-in and custom generators available within the current Rails application, categorized by their type (e.g., Rails, Active Record, Test Unit).
CUSTOM GENERATORS
Developers can extend Rails' code generation capabilities by creating their own custom generators. This allows for automation of domain-specific boilerplate code or adherence to unique project standards, further enhancing development workflow efficiency.
HISTORY
The concept of code generation has been integral to Ruby on Rails since its early versions. Initially, generators were invoked via script/generate (pre-Rails 3). With the release of Rails 3, the command was unified under the main rails executable, becoming rails generate. This evolution solidified its role as a core tool for adhering to Rails' "Convention over Configuration" principle, significantly boosting developer productivity by automating repetitive setup tasks and ensuring consistent project structure across applications.
SEE ALSO
rails destroy, rails new, rails server, rails db:migrate, rails console