How does Rails handle different environments and what are they used for?

Beginner

Answer

Rails provides three default environments:

  • Development: Reloading, verbose errors, debugging tools
  • Test: Isolated testing, fixtures, transaction rollback
  • Production: Optimized performance, caching, error monitoring
# Check current environment
Rails.env.development?  # true/false
Rails.env  # "development"

# Environment-specific code
if Rails.env.production?
  # Production-only code
end

# Custom environments
RAILS_ENV=staging rails server

Each environment has its own configuration file in config/environments/ and database configuration.