Sitemap
Ronak Bhatt

Ronak Bhatt

A software engineer with solid experience mostly with Ruby on rails and Javascripts.

Essential Rails 7 & 8 Commands Guide

5 min readJan 27, 2025

--

Press enter or click to view image in full size

Project Setup and Management

Creating a New Rails Application

# Basic Rails app with default configurations
rails new myapp

# Rails app with specific database
rails new myapp --database=postgresql
rails new myapp --database=mysql
rails new myapp --database=sqlite3

# API-only application
rails new myapp --api

# Minimal application without default gems
rails new myapp --minimal

# Rails 7+ with specific frontend configurations
rails new myapp --css tailwind
rails new myapp --css bootstrap
rails new myapp --css bulma
rails new myapp --javascript esbuild
rails new myapp --javascript webpack
rails new myapp --javascript importmap

# Skip specific components
rails new myapp --skip-test
rails new myapp --skip-system-test
rails new myapp --skip-active-storage
rails new myapp --skip-action-mailbox
rails new myapp --skip-action-text

Database Operations

# Create database
rails db:create

# Run all pending migrations
rails db:migrate

# Rollback last migration
rails db:rollback STEP=1

# Reset database (drop, create, migrate)
rails db:reset

# Load seed data
rails db:seed

# Drop database
rails db:drop

# Advanced Database Commands
rails db:migrate:status # Check migration status
rails db:version # Show current schema version
rails db:schema:load # Load schema.rb
rails db:structure:load # Load structure.sql
rails db:setup # Create, load schema, and seed
rails db:prepare # Prepare test database
rails db:environment:set RAILS_ENV=production # Set environment

# Database Maintenance
rails db:schema:cache:clear # Clear schema cache
rails db:schema:cache:dump # Create schema cache
rails db:migrate:down VERSION=20230101000000 # Revert specific migration
rails db:migrate:up VERSION=20230101000000 # Run specific migration

Generating Components

Model Generation

# Generate model with attributes
rails generate model User name:string email:string:uniq age:integer

# Generate model with references
rails generate model Comment content:text user:references post:references

# Generate model with validations
rails generate model Product name:string{30} price:decimal{10,2}

# Advanced Model Generation
rails generate model User admin:boolean default:false
rails generate model Article slug:string:uniq title:string{100} body:text published_at:datetime
rails generate model Profile user:belongs_to bio:text social_links:json
rails generate model Category name:string position:integer:index

Controller Generation

# Generate controller with actions
rails generate controller Users index show create update destroy

# Generate API controller
rails generate controller api/v1/Users index show --skip-template-engine

# Generate controller with specific namespace
rails generate controller Admin::Dashboard index

# Additional Controller Options
rails generate controller Posts --skip-routes
rails generate controller Comments --skip-helper
rails generate controller Admin::Products --skip-assets
rails generate controller Api::V2::Users --skip-template-engine --api

Advanced Generators

# Generate mailer
rails generate mailer UserMailer welcome_email password_reset

# Generate job
rails generate job process_payment
rails generate job send_notifications --queue urgent

# Generate channel
rails generate channel room speak

# Generate serializer
rails generate serializer user name email role

# Generate stimulus controller
rails generate stimulus form_validation

Testing Framework

# Run all tests
rails test

# Run specific tests
rails test test/models/user_test.rb
rails test test/controllers
rails test test/integration

# Run tests with specific configuration
rails test -v # Verbose output
rails test --fail-fast # Stop on first failure
rails test --seed 12345 # Run with specific seed

# System Tests
rails test:system # Run all system tests
rails generate system_test users # Generate system test
rails test:system -v # Verbose system tests

# Performance Tests
rails generate performance_test browsing
rails test:performance # Run performance tests

# Parallel Testing
rails test:parallel # Run tests in parallel
rails test:prepare # Prepare parallel test database

Get Ronak Bhatt’s stories in your inbox

Join Medium for free to get updates from this writer.

Development Tools and Debugging

# Server Commands
rails server -p 4000 # Run on specific port
rails server -b 0.0.0.0 # Bind to all interfaces
rails server -e production # Run in specific environment
rails server -d # Run as daemon

# Console Variations
rails console --sandbox # Run console in sandbox mode
rails console -e production # Production console
rails console test # Test environment console

# Debugging and Information
rails stats # Code statistics
rails notes # Show TODO/FIXME/OPTIMIZE comments
rails middleware # Show middleware stack
rails runner 'puts User.count' # Run Ruby code

# Routes Information
rails routes -g users # Show routes matching pattern
rails routes -c Users # Show routes for controller
rails routes --expanded # Display routes in expanded format

Asset Pipeline and Webpacker

# Asset Pipeline Commands
rails assets:precompile # Compile assets
rails assets:clean # Remove old compiled assets
rails assets:clobber # Remove compiled assets
rails assets:environment # Load asset environment
rails assets:precompile:all # Compile all assets

# Webpacker Specific (if using Webpacker)
rails webpacker:install # Install Webpacker
rails webpacker:compile # Compile JavaScript packs
rails webpacker:clean # Clean old compiled packs
rails webpacker:clobber # Remove compiled packs

Production and Deployment

# Production Setup
rails credentials:edit --environment production
rails db:migrate RAILS_ENV=production
rails assets:precompile RAILS_ENV=production

# Secret Management
rails secret # Generate secret key
rails credentials:show # Show credentials
rails encrypted:edit config/master.key # Edit master key

# Cache Management
rails dev:cache # Toggle development caching
rails tmp:cache:clear # Clear tmp cache
rails tmp:clear # Clear all tmp files

Rails 7+ Modern Frontend

# Hotwire/Turbo Installation
rails turbo:install
rails turbo:install:redis

# Stimulus Installation
rails stimulus:install

# Import Map Commands
bin/importmap pin @hotwired/turbo-rails
bin/importmap pin @hotwired/stimulus
bin/importmap pin lodash --download
bin/importmap json

# CSS Processing
rails css:install:tailwind
rails css:install:bootstrap
rails css:install:postcss
rails css:install:sass

Database Analysis and Maintenance

# Schema Management
rails db:schema:dump # Create schema.rb
rails db:structure:dump # Create structure.sql
rails db:version # Show schema version

# Database Analysis
rails db:analyze # Analyze database (PostgreSQL)
rails db:validate # Validate foreign keys
rails db:seed:replant # Delete data and reseed

# Data Migration
rails db:environment:set # Set database environment
rails db:_dump # Create backup of structure
rails db:abort_if_pending_migrations # Check pending migrations

Common Troubleshooting Commands

# Reset everything
rails db:drop db:create db:migrate

# Clear temporary files and logs
rails log:clear tmp:clear

# Check environment
rails about

# Validate database
rails db:validate

# Show middleware
rails middleware

# Check routes
rails routes | grep users

Remember to:

  • Always backup your database before major operations
  • Use version control for all changes
  • Test in development before applying to production
  • Keep gems updated and check for security vulnerabilities regularly
  • Monitor application logs for errors and performance issues

The commands above work with Rails 7 and 8, though some newer features might be version-specific. Always check the Rails documentation for the most up-to-date information about specific commands and their options.

--

--

Ronak Bhatt

Written by Ronak Bhatt

A software engineer with solid experience mostly with Ruby on rails and Javascripts.