Rails Commands

Ruby on Rails Command Reference

Welcome to my snippets notes on Ruby on Rails commands! This guide covers essential commands for Rails development, from project setup to deployment. I've organized these commands by category for easy reference during development.

Getting Started with Rails

Rails provides a powerful command-line interface that makes development faster and more efficient. Learning these commands will significantly improve your workflow!

Creating New Applications

ruby snippet
1# Standard Rails application
2rails new my_app
3
4# Rails with PostgreSQL database
5rails new my_app --database=postgresql
6
7# API-only application (no views)
8rails new my_app --api
9
10# Rails 7+ with modern frontend options
11rails new my_app --css tailwind
12rails new my_app --javascript esbuild
13
14 # Rails 7+ with Hotwire/Turbo
15rails new my_app --turbo
16
17# Skip test files and other boilerplate
18rails new my_app --skip-test --skip-action-mailer
19
20# Skip all default configurations
21rails new my_app --minimal
22
When creating a new Rails application, you can customize it extensively with command-line options. The --database flag lets you choose your preferred database engine, while --css and --javascript flags configure your frontend stack.

Database Management

Managing your database is a critical part of Rails development. Here are the essential commands:
ruby snippet
1# Set up your database
2rails db:create # Create the database
3rails db:migrate # Run pending migrations
4rails db:seed # Load seed data
5
6# Database maintenance
7rails db:rollback STEP=1 # Undo last migration
8rails db:reset # Drop, recreate, and migrate database
9rails db:version # Display current schema version
10
11# Advanced Database Commands
12rails db:migrate:status # Check migration status
13rails db:version # Show current schema version
14rails db:schema:load # Load schema.rb
15rails db:structure:load # Load structure.sql
16rails db:setup # Create, load schema, and seed
17rails db:prepare # Prepare test database
18rails db:environment:set RAILS_ENV=production # Set environment
Always back up your database before running destructive commands like db:reset or db:drop in production environments. These commands will permanently delete your data!

Generating Rails Components

Rails generators help you create components with proper naming conventions and boilerplate code.

Model Generation

ruby snippet
1# Basic model with attributes
2rails generate model Product name:string price:decimal{10,2} description:text
3
4# Model with associations
5rails generate model Review content:text rating:integer product:references user:references
6
7# Model with validations and indexes
8rails generate model User email:string:uniq username:string{30} admin:boolean
The generate model command creates several files: the model itself, a database migration, test files, and fixtures. This ensures consistency across your application.

Controller Generation

ruby snippet
1# Standard controller with actions
2rails generate controller Products index show new create edit update destroy
3
4# API controller
5rails generate controller api/v1/Products index show create update destroy --skip-template-engine
6
7# Namespaced controller
8rails generate controller Admin::Dashboard index stats users

Testing Your Application

Rails comes with a comprehensive testing framework. Here are commands to run your tests:
ruby snippet
1# Run all tests
2rails test
3
4# Run specific test files
5rails test test/models/user_test.rb
6rails test test/controllers/products_controller_test.rb
7
8# Run system tests (browser-based)
9rails test:system
"If you're not testing, you're doing it wrong." - Rails community wisdom

Development Tools

These commands help during the development process:
ruby snippet
1# Start the Rails server
2rails server # Default port 3000
3rails server -p 4000 # Custom port
4rails server -b 0.0.0.0 # Accessible from network
5
6# Rails console
7rails console # Regular console
8rails console --sandbox # Changes won't be saved to database
9
10# Examine routes
11rails routes # List all routes
12rails routes -g products # Filter routes by pattern
For more detailed information about Rails commands, refer to the official Rails Guides and API documentation at https://guides.rubyonrails.org/

Asset Pipeline and Frontend

Rails 7+ offers modern frontend tooling options:
ruby snippet
1# Asset compilation
2rails assets:precompile # Compile assets for production
3
4# Hotwire/Turbo setup
5rails turbo:install
6rails turbo:install:redis
7
8# Import map for JavaScript dependencies
9bin/importmap pin lodash
10bin/importmap pin chart.js --download
Always precompile assets before deploying to production. Failing to do so can result in missing styles and JavaScript functionality in your application.

Production and Deployment

Preparing your application for production:
ruby snippet
1# Credential management
2rails credentials:edit --environment production
3
4# Production setup
5rails db:migrate RAILS_ENV=production
6rails assets:precompile RAILS_ENV=production
7
8# Secret key generation
9rails secret # Generate a secure random key

Troubleshooting Common Issues

When things go wrong, these commands can help:
ruby snippet
1# Reset database completely
2rails db:drop db:create db:migrate db:seed
3
4# Clear temporary files and logs
5rails log:clear tmp:clear
6
7# Check environment and configuration
8rails about
9
10# Validate database consistency
11rails db:validate
Having trouble with your Rails application? Try running rails about to see a summary of your Rails environment, which can help identify version mismatches or configuration issues.

Best Practices

Remember these key principles when working with Rails:
  1. Use version control for all your code changes
  2. Write tests for your application features
  3. Keep your gems updated for security and performance
  4. Follow Rails conventions for easier maintenance
  5. Monitor your logs for errors and performance issues
Rails follows the "Convention over Configuration" principle. By adhering to Rails naming conventions and patterns, you'll write less code and encounter fewer bugs.