Framework

Seed Rails with Realistic Test Data via API

The Problem

Rails has db/seeds.rb, but most seed files are a mess of create! calls with hardcoded strings. Gems like Faker help with realistic values, but they do not manage foreign key relationships for you. You still manually chain creates and pass IDs between models.

For a typical Rails app with users, posts, and comments, that means three nested loops with careful ordering. Miss one association and your seed file crashes.

The Solution: MockHero API

MockHero generates linked, realistic data from a single HTTP call. Define your Rails models as a MockHero schema, use ref fields for associations, and insert the JSON through ActiveRecord.

Quick Setup

curl -X POST https://api.mockhero.dev/api/v1/generate \
  -H "x-api-key: mh_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
  "tables": [
    {
      "name": "users",
      "count": 8,
      "fields": [
        { "name": "id", "type": "uuid" },
        { "name": "name", "type": "full_name" },
        { "name": "email", "type": "email" }
      ]
    },
    {
      "name": "posts",
      "count": 24,
      "fields": [
        { "name": "id", "type": "uuid" },
        { "name": "user_id", "type": "ref", "params": { "ref": "users.id" } },
        { "name": "title", "type": "sentence" },
        { "name": "body", "type": "paragraphs" },
        { "name": "published_at", "type": "datetime" }
      ]
    }
  ],
  "format": "json"
}'

Step-by-Step Guide

1. Install dependencies

Add gem "httparty" to your Gemfile and run bundle install.

2. Get your MockHero API key

Sign up at mockhero.dev/sign-up.

3. Write the seed script

Edit db/seeds.rb to call the MockHero API and loop through the response, creating records with User.create! and Post.create!.

4. Run it

rails db:seed

5. Verify in Rails console

rails console
Post.first.user.name

Complete Example

The db/seeds.rb calls MockHero's API and inserts users and posts with valid foreign key references through ActiveRecord.

Why MockHero vs Faker / FactoryBot

  • No factories to write — JSON schema replaces Ruby factory classes.
  • Associations handledref fields replace association :user wiring.
  • CI-friendly — deterministic seeds with the seed parameter for reproducible test suites.

Get Started

Free tier, 1,000 rows/month. Sign up at mockhero.dev and run rails db:seed with production-quality data.

M

MockHero Team

Guides and tutorials for generating realistic test data with the MockHero API.

Start generating test data for free

1,000 rows/month on the free tier. No credit card required.

Get Your API Key

Related Articles