In a Rails 8.1 app, how do you seed demo learners only in development while countries and plans load in every environment, including production?
answer
- one seeds file for every environment
- branch on Rails.env
- db/seeds/<env>.rb by convention
- db:prepare can seed production's new database
- config/ci.rb seeds the test database
basics
~20 sRails loads the same db/seeds.rb in every environment, so the file must branch itself: reference data such as countries and plans at the top, demo learners behind Rails.env.development? or in a db/seeds/development.rb that seeds.rb loads only there.
solid answer
~40 sRails has one seeds file and no per-environment seed loading of its own; `bin/rails db:seed` loads `db/seeds.rb` whatever `RAILS_ENV` is. So reference data that every environment needs (countries, the free and pro plans) goes first, written idempotently, and demo data goes behind `if Rails.env.development?`, or into `db/seeds/development.rb` that `db/seeds.rb` loads with `load Rails.root.join("db/seeds/#{Rails.env}.rb")` when the file exists. The guard matters because seeds do reach production: the generated `bin/docker-entrypoint` runs `db:prepare` before `bin/rails server`, and on a brand-new database that loads the schema and runs `db/seeds.rb`. Demo learners with known passwords in production would be accounts anyone can log into. Rails 8.1's generated `config/ci.rb` also runs `db:seed:replant` with `RAILS_ENV=test`, so the test branch must work too.
code
ruby · 5 lines# db/seeds.rb
load Rails.root.join("db/seeds/reference.rb")
env_file = Rails.root.join("db/seeds/#{Rails.env}.rb")
load env_file if File.exist?(env_file)go deeper
Know that the same db/seeds.rb runs in every environment, so development-only data must sit behind a Rails.env check.
Explain the usual layout: shared reference data first, then an environment file or guarded block, each part idempotent.
Point out how seeds reach production through db:prepare on a new database and test through config/ci.rb, and keep secrets out of seeds.
Decide which data is reference data owned by the app and which is demo or operational data owned elsewhere, and set the rule for each.
## One file, every environment A language-learning app needs three kinds of seed data: | Data | Development | Test | Production | |---|---|---|---| | countries (ISO code, name) | yes | yes | yes | | default plans (free, pro) | yes | yes | yes | | demo learners with progress | yes | no | **never** | Rails offers **one** seeds file. `bin/rails db:seed` calls `Rails.application.load_seed`, which loads `db/seeds.rb`; there is no built-in `db/seeds/production.rb` lookup and no per-environment setting for it. Any split by environment is code you write inside `db/seeds.rb`. ## Two common shapes **Branch in place**, for small files: ```ruby # db/seeds.rb load Rails.root.join("db/seeds/reference.rb") if Rails.env.development? learner = Learner.find_or_create_by!(email_address: "[email protected]") do |l| l.password = "demo-password" end end ``` **One file per environment**, a convention many teams adopt: ```ruby # db/seeds.rb load Rails.root.join("db/seeds/reference.rb") env_file = Rails.root.join("db/seeds/#{Rails.env}.rb") load env_file if File.exist?(env_file) ``` Either way: 1. **Reference data runs everywhere** and comes first, because demo learners point at countries and plans. 2. **Environment data runs after it**, guarded by `Rails.env`. 3. **Every part stays idempotent** (`find_or_create_by!` on stable keys), because each part can run many times. ## How seeds reach production It is tempting to think production never runs seeds. A generated Rails 8.1 app proves otherwise: - The generated **`bin/docker-entrypoint`** runs `./bin/rails db:prepare` when the container's command is `./bin/rails server`. - `db:prepare`, on a database that does not exist yet or has no tables, loads the schema and then **runs `db/seeds.rb`** for the primary database. So the very first boot of production, or of a fresh staging copy, executes your seeds file with `RAILS_ENV=production`. If demo learners are not guarded, production gets accounts with passwords committed to the repository. The same goes for anything else environment-specific, such as an admin account: in production, take its email address and password from credentials or environment variables rather than hard-coding them, or create it by hand. ## How seeds reach the test environment Rails 8.1 generates **`config/ci.rb`**, the step list that `bin/ci` runs. One of its default steps is `Tests: Seeds`, which runs `env RAILS_ENV=test bin/rails db:seed:replant`. Its purpose is to prove the seeds file still loads cleanly, but it also means the `test` branch of your seeds runs on every CI run. Keep that branch light: tests normally get their data from fixtures or factories, not from seeds. ## Order and dependencies inside the files Once seeds are split across files, order matters. `load` runs each file top to bottom at the point it is called, so the reference file must come before anything that points at its rows. Inside an environment file, look reference rows up with **`find_by!`** rather than `find_by`: if the country or plan is missing, `ActiveRecord::RecordNotFound` stops the run at the real cause instead of creating learners with a `nil` country that fail validation three lines later. ## Keeping demo data rerunnable Demo data has to stay idempotent too, which rules out a few tempting habits: - **No random keys.** Generating email addresses with a random-data library creates new learners on every run; use fixed addresses such as `[email protected]` as the lookup key and randomise only attributes set inside the block. - **Bounded volume.** A hundred learners make the dashboard look real; a hundred thousand make every reseed slow. Large performance datasets belong in a separate script run on demand. - **Reserved domains.** Demo email addresses on `example.com` cannot reach a real inbox if a mailer fires during seeding. - **Rebuild, do not patch.** When the demo set changes shape, run `bin/rails db:seed:replant` locally rather than writing code to tidy old demo rows. ## Checklist - Use `Rails.env.development?`, `Rails.env.test?` and `Rails.env.production?` for the guards; avoid branching on hostnames or custom variables nobody remembers. - Never commit real credentials in seeds; demo passwords are acceptable only behind a development guard. - Keep reference data in its own file so a production run can load exactly that and nothing else. - Make large demo sets quick to rebuild with `bin/rails db:seed:replant`, which empties the tables first. - Read the seeds file as production code: it can run there.
- How do you create the first admin account in production without committing a password?Read it at seed time from somewhere secret: `Rails.application.credentials.dig(:admin, :password)` or an environment variable set for that one run, and skip the step when the value is missing. Alternatively create the account by hand in a console session after the first deploy. Either way, nothing secret lives in `db/seeds.rb`.
- Why keep the test branch of seeds light?Tests load their own data from fixtures or factories and usually run inside transactions that roll back. Seeded rows would sit under every test and make results depend on them. The CI seeds step exists to prove the file runs, not to supply test data.
- Rails loads db/seeds/production.rb automatically in production
- seeds never run in production, so demo users are harmless
- put demo learners in fixtures so db:seed loads them in development
- commit the admin password in seeds.rb for the first deploy
- db:seed refuses to run in production
Assumes Rails 8.1: the generated config/ci.rb with its db:seed:replant step is new in 8.1, and the generated Docker entrypoint runs db:prepare before the server starts.