# seed_dump for Rails Database Seed Generation

> seed_dump is a Ruby gem that exports existing Active Record data as executable Rails seed code, with filtering, batching, STI handling, and several output formats. It is best suited to small, reviewable development datasets rather than unrestricted production-data exports.

**rroblak/seed_dump** — Rails task to dump your data to db/seeds.rb

- Repository: https://github.com/rroblak/seed_dump
- Stars: 1,411 · Forks: 221
- Language: Ruby
- License: MIT
- Published: 2026-10-16 · Updated: 2026-10-16 · Language: en
- Canonical page: https://hysenlabs.com/projects/rroblak-seed-dump

## Why seed_dump Exists in a Rails Application

seed_dump solves a recurring Rails development problem: turning records that already exist in an Active Record database into Ruby code that can recreate selected records through application models. Its default output is written to `db/seeds.rb`, and its Rake interface is named `db:seed:dump`. The intended audience is Rails developers who need sample data for development, demonstrations, or repeatable application setup without manually writing seed statements.

This is not a complete database backup tool. The generated file contains selected Active Record records and does not claim to reproduce schema migrations, database configuration, raw SQL, or every database feature. The distinction matters because the normal output uses calls such as `Product.create!` and `User.create!`. Replaying a dump therefore depends on the target application having compatible models, associations, validations, and database schema.

The project is aimed at Rails applications rather than arbitrary databases. The README states compatibility with Rails 4 through Rails 8 and later. Applications on Rails 3 or earlier are directed to version 0.5.3. Within supported applications, the main audience is a developer who has populated a local database and wants a seed artifact that another developer can inspect and run through the Rails environment.

## How Active Record Rows Become Rails Seed Code

The central API is `SeedDump.dump`. It accepts an Active Record model class or relation and produces Ruby seed statements as a string. The Rake task provides the same basic operation while adding model selection and task-only controls. By default, the Rake task writes to `db/seeds.rb`. Without a file option in the console, `SeedDump.dump` returns the generated dump as a string.

The output represents attributes as Ruby hash entries and groups records under model calls. This means the generated code is executed through Active Record rather than emitted as a portable SQL archive. The documented default exclusion list is `[:id, :created_at, :updated_at, :created_on, :updated_on]`. A developer can override that list, and the Rake task can request every column with `INCLUDE_ALL=true`. Including identifiers can matter when generated records need to retain specific IDs.

Rows are processed in batches, with `batch_size` defaulting to 1000. The README recommends decreasing this value when memory use is too high and increasing it when dumping is too slow. Models are also placed in dependency order based on foreign-key relationships so parents are generated before children. These are generation-time behaviors in the output, not guarantees that running the file will succeed under every schema or association configuration.

Active Record relations can narrow the console input directly. Passing `User.where(is_admin: false)` produces output for that relation, while `User.limit(5)` applies the requested relation limit. An individual Active Record object is not supported. The README specifically says that any relation of rows can be dumped, but an individual object cannot.

## Installing seed_dump and Creating db/seeds.rb

The Gemfile method is the normal Bundler installation path for a Rails application. Add the gem to the application Gemfile:

```ruby
gem 'seed_dump'
```

The README does not show a subsequent Bundler command, so there is no documented `bundle install` example to copy. A developer outside Bundler can use the documented RubyGems command:

```bash
gem install seed_dump
```

Once the gem is available to the application, run the default Rake task from the Rails project environment:

```bash
rake db:seed:dump
```

The default operation is intended to dump all models that contain data and overwrite `db/seeds.rb`. Because it reads existing database contents, the result depends on the current application database. If the database is empty, the README does not promise that the task will create a representative set of records.

For a smaller first result, restrict the output to one model and one record:

```bash
rake db:seed:dump MODELS=User LIMIT=1
```

The documentation shows this producing a `User.create!` call for one user. Review the generated file before running it elsewhere. The README example includes a plaintext password value, which illustrates attribute serialization rather than a data-handling policy. Generated seed files can contain credentials, tokens, personal information, or other sensitive application data when those columns are present in the source table.

## Filtering Models, Attributes, and Seed File Locations

The Rake task can discover models and manage task-level filtering. `MODELS` accepts a comma-separated model list, including namespaced Rails engine models such as `EngineName::ModelName`. `MODELS_EXCLUDE` removes named models. `LIMIT` applies a maximum per model, while `MODEL_LIMITS` assigns different limits where one global cap would damage relationship data.

For example, the following preserves every Teacher record, limits Students to 50, and applies a limit of 10 to other models:

```bash
rake db:seed:dump LIMIT=10 MODEL_LIMITS="Teacher:0,Student:50"
```

Attribute exclusions work across both interfaces, although their input syntax differs. The Rake task takes a comma-separated value, while the console takes an array of symbols:

```bash
rake db:seed:dump EXCLUDE=name,age
```

The output destination can also be changed. This directs the file beneath `db/seeds`, avoiding replacement of the conventional default file:

```bash
rake db:seed:dump FILE=db/seeds/users.rb
```

`APPEND=true` appends rather than overwrites the selected file. This is useful for separately generated fixtures, but it also means repeated execution can accumulate duplicate seed statements unless the file is cleared or managed deliberately. The console expresses the same behavior through an option:

```ruby
SeedDump.dump(User, file: 'db/seeds.rb', append: true)
```

If the output file is meant for review and reuse, `FILE` is the first control to consider. It separates generated data from application startup code and makes accidental execution of the complete development dataset less likely.

## STI Models, Insert Modes, and Relationship Trade-offs

Single Table Inheritance requires care because subclasses can have class-specific enum definitions or attributes. By default, seed_dump deduplicates STI models and writes them through the base class. Setting `group_sti_by_class` to `true` changes the output so records are grouped by their actual class, such as `Dog` or `Cat`, rather than the base `Animal` class. This is relevant when base-class output would lose subclass-specific meaning.

The default `create!` format invokes Active Record creation and is tied to model behavior. Two optional formats change that relationship. `INSERT_ALL=true` generates Rails 6+ `insert_all` calls for bulk insertion that bypass validations and callbacks. `UPSERT_ALL=true` generates `upsert_all`, preserves record IDs, and handles conflicts by updating existing records. The latter is intended for maintaining foreign-key relationships and producing idempotent seed files. It automatically includes `id`.

These options should be selected from the purpose of the seed file, not merely for speed. `insert_all` avoids callbacks and validations, so a seed file that looks equivalent to `create!` may behave differently when application invariants are implemented in those layers. `upsert_all` changes the intended database state by updating conflicting records rather than only creating them. The README does not document rollback for generated files or a data classification policy, so both require review outside the gem's documented feature set.

## Seed File Formats Compared with activerecord-import

A concrete alternative is `activerecord-import`, a separate gem associated with bulk import behavior. seed_dump can emit output in the format required by that project through `import: true`, or through the task-level `IMPORT` option. This relationship makes activerecord-import relevant, but the two projects serve different stages.

seed_dump starts with an existing Active Record database and generates source code for fixtures. Its outputs include ordinary `create!` statements, Rails `insert_all` or `upsert_all` calls, and an `activerecord-import` format. It also selects records, excludes columns, orders models, and manages STI output. The artifact is intended to be read and potentially committed as seed data.

`activerecord-import` is presented by seed_dump's README as the target library for its import-oriented output. The README does not describe that gem's broader APIs, so this review does not attribute additional features to it. The established difference is output format: seed_dump performs database-to-Ruby generation and delegates one optional execution format to activerecord-import. A team that already has an import pipeline may enable that output, while a team needing filtered Rails seed source can retain seed_dump's default Ruby format.

## Maintenance, Compatibility, and MIT Upgrade Work

The repository is not archived, and its last push was on 2026-05-13. Releases include v3.4.2 on 2026-05-13, v3.4.1 on 2025-12-24, and v3.4.0 on 2025-12-03. The README states compatibility from Rails 4 through Rails 8 and later, while Rails 3 and earlier users are directed to version 0.5.3. This version range should be checked against the application's Rails release before adoption, particularly because several execution formats require Rails 6 or later.

The project is distributed under the MIT license and includes an `MIT-LICENSE` file. The practical upgrade cost is not limited to changing a gem version. Generated output can depend on task options, and code using `insert_all` or `upsert_all` will not suit an application below Rails 6. A team changing from excluded identifiers to `upsert_all` should also expect the generated file to retain `id` values.

The repository includes `Appraisals`, `gemfiles/`, a `Rakefile`, and `spec/`, but the README does not document an upgrade procedure or a per-version compatibility table. It also does not state a migration policy for old generated seed files. The concrete pre-upgrade check is to regenerate a small fixture in the application, compare it with the committed seed code, and execute it against a disposable database. The gem version, application Rails version, output mode, and generated Ruby should be checked together rather than treating the dependency update as an isolated change.

## Conclusion

Rails developers who need compact, reviewable development fixtures should adopt seed_dump, while teams seeking a general database backup or production migration system should not. Before committing generated output, verify the selected models, excluded attributes, STI grouping, foreign-key ordering, and whether the chosen import mode bypasses model behavior.

## FAQ

### How do I install seed_dump in a Rails application?

Add `gem 'seed_dump'` to the Gemfile, or install it with `gem install seed_dump`. The README does not document a separate Bundler command for the Gemfile route.

### How do I export only selected models and columns with seed_dump?

Use `MODELS` to select models and `EXCLUDE` to remove columns. A command can limit the task to users while excluding one field with `rake db:seed:dump MODELS=User EXCLUDE=password`.

### Does seed_dump preserve foreign-key relationships and STI subclasses?

The Rake task orders models by foreign-key dependencies, while `group_sti_by_class` can group STI records by actual subclass. `upsert_all` is also documented as preserving IDs and updating conflicting records for relationship-oriented seed files.

## Sources

- [Issues](https://github.com/rroblak/seed_dump/issues)
- [License: MIT](https://github.com/rroblak/seed_dump/blob/master/LICENSE)
- [README](https://github.com/rroblak/seed_dump/blob/master/README.md)
- [Releases](https://github.com/rroblak/seed_dump/releases)
- [rroblak/seed_dump on GitHub](https://github.com/rroblak/seed_dump)

---

Hysen Labs editorial analysis, written from the project's own repository and release notes. Cite the canonical page: https://hysenlabs.com/projects/rroblak-seed-dump
