Skip to content

Ruby on Rails Integration

DevDb Pro provides zero-config database discovery for Ruby on Rails projects, automatically reading your config/database.yml to connect to your database.

Zero-Config Discovery

DevDb automatically detects Rails databases from your project configuration.

How It Works

  1. Open a Rails project in VS Code
  2. DevDb reads config/database.yml
  3. Extracts database config for current environment
  4. Auto-connects to your database

Requirements

  • Rails project with config/database.yml
  • Standard Rails database configuration
  • Database server running and accessible

Supported Databases

DevDb works with these Rails database adapters:

  • SQLite (sqlite3)
  • MySQL (mysql2)
  • PostgreSQL (postgresql)

Configuration Examples

SQLite

config/database.yml:

yaml
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

DevDb automatically connects to the SQLite file.

MySQL

config/database.yml:

yaml
development:
  adapter: mysql2
  encoding: utf8mb4
  database: myapp_development
  pool: 5
  username: root
  password: secret
  host: 127.0.0.1
  port: 3306

DevDb reads credentials and connects to MySQL.

PostgreSQL

config/database.yml:

yaml
development:
  adapter: postgresql
  encoding: unicode
  database: myapp_development
  pool: 5
  username: postgres
  password: secret
  host: localhost
  port: 5432

DevDb connects to PostgreSQL automatically.

Environment Detection

DevDb reads the Rails environment to select the right configuration:

Default Behavior

By default, DevDb uses the development environment:

yaml
development:    # ← DevDb uses this
  adapter: postgresql
  database: myapp_development

test:
  adapter: postgresql
  database: myapp_test

production:
  adapter: postgresql
  database: myapp_production

Environment Variables

If RAILS_ENV is set, DevDb uses that environment:

bash
export RAILS_ENV=test  # DevDb uses 'test' config

Multiple Databases

Rails 6+ supports multiple databases:

config/database.yml:

yaml
development:
  primary:
    adapter: postgresql
    database: myapp_primary
    username: postgres
    password: secret

  analytics:
    adapter: mysql2
    database: myapp_analytics
    username: root
    password: secret

DevDb detects and lists both databases:

  • myapp_primary (PostgreSQL)
  • myapp_analytics (MySQL)

Switch between them in the DevDb sidebar.

ERB Templates

DevDb supports ERB in database.yml:

config/database.yml:

yaml
development:
  adapter: postgresql
  database: <%= ENV['DB_NAME'] || 'myapp_development' %>
  username: <%= ENV['DB_USER'] || 'postgres' %>
  password: <%= ENV.fetch('DB_PASSWORD') %>
  host: <%= ENV['DB_HOST'] || 'localhost' %>

DevDb processes ERB and resolves environment variables.

Features

Database Client

Full Database Client features:

  • Browse tables and data
  • Inline editing
  • Schema viewer
  • JSON preview

Quick Table Access

Use Cmd+K Cmd+G (Mac) or Ctrl+K Ctrl+G (Windows/Linux):

  • Quickly open any ActiveRecord model table
  • Search across all databases
  • Instant navigation

Context Menu

Right-click table/model names in code:

  • "Open table at cursor" - View table in DevDb
  • Works with model class names
  • Works with custom table names

Example:

ruby
# Right-click "User" and select "DevDb > Open table at cursor"
class User < ApplicationRecord
  # ...
end

# Also works with custom table names
class Product < ApplicationRecord
  self.table_name = 'products'
end

Data Export

Export table data:

  • JSON format - For seeds or testing
  • SQL format - For migrations or backups
  • Copy to clipboard or save to file

Workflow Examples

Example 1: Inspect Models

After creating models and running migrations:

  1. Run migrations: rails db:migrate
  2. Open DevDb (Cmd+K Cmd+D or Ctrl+K Ctrl+D)
  3. Browse tables to verify schema
  4. Check indexes in Schema tab
  5. Add test data directly

Example 2: Debug Data Issues

When investigating data problems:

  1. Open relevant model in code
  2. Right-click model name → "Open table at cursor"
  3. Browse actual data in database
  4. Edit test records to reproduce issue
  5. Verify fix by refreshing data

Example 3: Create Seed Data

Generate seed data:

  1. Create test data in DevDb
  2. Export as SQL using Data Export
  3. Add to db/seeds.rb:
ruby
# db/seeds.rb
ActiveRecord::Base.connection.execute(
  File.read('path/to/export.sql')
)
  1. Run: rails db:seed

Example 4: Migration Verification

After writing migrations:

  1. Apply migration: rails db:migrate
  2. Refresh DevDb connection
  3. View schema changes in Schema tab
  4. Verify constraints and indexes
  5. Test with sample data

Rails-Specific Features

ActiveRecord Table Names

DevDb handles Rails table naming conventions:

Default naming (pluralized):

ruby
class Product < ApplicationRecord  # Table: products
end

Custom naming:

ruby
class Product < ApplicationRecord
  self.table_name = 'inventory_items'  # Table: inventory_items
end

DevDb recognizes both patterns.

Schema Migrations

View migration history:

  • Open schema_migrations table
  • See applied migrations
  • Check versions
  • Verify migration order

Rails Metadata Tables

DevDb shows all Rails metadata tables:

  • schema_migrations
  • ar_internal_metadata
  • ActiveStorage tables (if used)
  • ActionText tables (if used)

Troubleshooting

Database Not Detected

If DevDb doesn't find your Rails database:

  1. Check config/database.yml exists
  2. Verify YAML syntax is valid
  3. Ensure database is running
  4. Reload VS Code window

Connection Fails

If connection doesn't work:

  1. Test Rails connection: rails dbconsole
  2. Check credentials in database.yml
  3. Verify database server is accessible
  4. Review environment variables

ERB Processing Errors

If ERB templates cause issues:

  1. Test ERB manually: rails runner "puts Rails.configuration.database_configuration"
  2. Check environment variables are set
  3. Verify ERB syntax in database.yml

Docker/Container Setup

For containerized Rails:

  1. Expose database port in docker-compose.yml:
yaml
services:
  db:
    ports:
      - "5432:5432"  # PostgreSQL
  1. Use host port in database.yml:
yaml
development:
  host: localhost
  port: 5432
  1. DevDb connects to exposed port

Best Practices

Development

  1. Use separate databases for dev and test
  2. Keep sensitive data out of database.yml (use ERB + env vars)
  3. Add database dumps to .gitignore
  4. Export fixtures for test data

Team Collaboration

  1. Document database setup in README
  2. Share sample .env file (.env.example)
  3. Use consistent database versions
  4. Export seed data for onboarding

Security

  1. Never commit credentials to git
  2. Use ERB + environment variables for sensitive data:
yaml
production:
  password: <%= ENV['DATABASE_PASSWORD'] %>
  1. Exclude .devdbrc from version control
  2. Sanitize exports before sharing

Integration with Other Tools

Rails Console

Use DevDb alongside Rails Console:

  • DevDb for quick visual inspection
  • Rails Console for complex queries
  • Export from DevDb for scripting

ActiveAdmin

Combine with ActiveAdmin:

  • DevDb for raw data access
  • ActiveAdmin for admin interface
  • Quick verification of admin changes

Testing

Use DevDb for test data:

  1. Create test data in DevDb
  2. Export as SQL
  3. Use in fixtures or factories:
ruby
# test/fixtures/users.yml
generated_from_devdb:
  # ... data from DevDb export

Database Cleaner

Verify cleaning with DevDb:

  1. Run tests with DatabaseCleaner
  2. Check in DevDb that tables are clean
  3. Debug cleanup issues visually

Environment-Specific Tips

Development

  • Use DevDb for quick data inspection
  • Edit seed data directly
  • Test associations by browsing relations

Test

  • Verify test database schema matches development
  • Check fixtures load correctly
  • Debug test failures by inspecting data

Production (Local Mirror)

  • Never connect DevDb to production directly
  • Use database dump for local inspection:
bash
pg_dump production_db | psql local_mirror
  • Browse mirror safely in DevDb

Next Steps

DevDb Pro - Zero-config database management for VS Code