# Ruby on Rails + PostgreSQL

> Ruby on Rails with PostgreSQL and Redis for background jobs.

- Category: Full Web Stacks
- Difficulty: intermediate
- Components: rails, postgres, redis
- Tags: rails, ruby, postgres, sidekiq
- Source: https://docker.recipes/full-stacks/rails-postgres

## Overview

Ruby on Rails is a full-stack web application framework written in Ruby that follows the Model-View-Controller (MVC) architectural pattern. Created by David Heinemeier Hansson in 2003, Rails emphasizes convention over configuration and the DRY (Don't Repeat Yourself) principle, enabling rapid development of database-backed web applications. Rails includes everything needed to create database-backed web applications according to the MVC pattern, with built-in support for routing, templating, Active Record ORM, and Action Mailer. This stack combines Rails with PostgreSQL as the primary database and Redis for session storage, caching, and background job processing through Sidekiq. PostgreSQL provides ACID-compliant relational data storage with advanced features like JSON columns, full-text search, and complex query capabilities that complement Rails' Active Record ORM. Redis serves dual purposes: acting as a high-performance cache to reduce database load and powering Sidekiq for asynchronous job processing, which is essential for handling tasks like email delivery, file processing, and API integrations without blocking web requests. This combination is ideal for startups building MVPs, established companies running production web applications, and development teams who need a proven stack that scales from prototype to enterprise. Rails' rapid development capabilities combined with PostgreSQL's reliability and Redis' performance create a robust foundation for modern web applications that need both developer productivity and production stability.

## Features

- Active Record ORM with PostgreSQL-specific features including JSON/JSONB column support and advanced query capabilities
- Sidekiq background job processing powered by Redis for handling asynchronous tasks like email delivery and file processing
- Rails Action Cable WebSocket support using Redis as the adapter for real-time features
- PostgreSQL full-text search integration with Rails' built-in search functionality
- Redis-backed Rails caching for fragment caching, page caching, and Active Support::Cache
- Rails database migrations with PostgreSQL-specific features like partial indexes and custom data types
- Action Mailer integration with Sidekiq for asynchronous email delivery
- Rails session storage in Redis for improved performance and horizontal scaling

## Use cases

- E-commerce platforms requiring complex product catalogs, inventory management, and background order processing
- Content management systems needing full-text search capabilities and real-time collaboration features
- SaaS applications with multi-tenant architecture using PostgreSQL's row-level security
- Social media platforms requiring real-time notifications, feed generation, and image processing jobs
- Financial applications needing ACID compliance for transactions and audit trails
- API-first applications serving mobile apps with Rails API mode and background data synchronization
- Enterprise web applications requiring complex reporting, data analytics, and scheduled job processing

## Prerequisites

- Docker and Docker Compose installed with at least 2GB available RAM for all services
- Port 3000 available for Rails web server access
- Basic knowledge of Ruby syntax and Rails conventions (MVC pattern, Active Record)
- Understanding of relational database concepts for PostgreSQL schema design
- Familiarity with environment variables for database credentials and Redis configuration
- Git repository with Gemfile including rails, pg, redis, and sidekiq gems

## docker-compose.yml

```yaml
services:
  web:
    build: .
    container_name: rails
    command: bundle exec rails server -b 0.0.0.0
    volumes:
      - .:/app
    environment:
      DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
      REDIS_URL: redis://redis:6379/0
    ports:
      - "3000:3000"
    depends_on:
      - postgres
      - redis
    networks:
      - rails

  postgres:
    image: postgres:16-alpine
    container_name: postgres
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - rails

  redis:
    image: redis:alpine
    container_name: redis
    networks:
      - rails

  sidekiq:
    build: .
    command: bundle exec sidekiq
    environment:
      DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
      REDIS_URL: redis://redis:6379/0
    depends_on:
      - postgres
      - redis
    networks:
      - rails

volumes:
  postgres_data:

networks:
  rails:
    driver: bridge
```

## .env

```bash
DB_NAME=rails
DB_USER=rails
DB_PASSWORD=changeme
```

## Usage notes

- Docs: https://guides.rubyonrails.org/
- Create Dockerfile with Ruby + bundler
- Run migrations: docker compose exec web rails db:migrate
- Access at http://localhost:3000
- Rails console: docker compose exec web rails c
- Sidekiq for background jobs (Action Mailer, Active Job)

## Troubleshooting

- Gem::Ext::BuildError during bundle install: Install build-essential and libpq-dev in Dockerfile before running bundle install
- PG::ConnectionBad - could not connect to server: Ensure postgres service starts before web service using depends_on and verify DATABASE_URL format
- Redis::CannotConnectError in Sidekiq: Check REDIS_URL environment variable format and ensure redis service is running before sidekiq
- ActiveRecord::PendingMigrationError: Run 'docker compose exec web rails db:create db:migrate' to set up database schema
- Sidekiq jobs not processing: Verify sidekiq service is running with 'docker compose ps' and check redis connection in Rails console
- Rails server binding to 127.0.0.1 instead of 0.0.0.0: Ensure server command includes '-b 0.0.0.0' flag for Docker container access

