A Rails 8.1 release adds a Family plan that production must have; does it belong in db/seeds.rb, a data migration, or a one-off task?
answer
- deploys migrate, they do not seed
- a migration runs once per database
- fresh databases load schema, skip old migrations
- seeds hold the canonical list
- one-off fixes belong in a task
basics
~20 sUsually both: a deploy runs migrations, not seeds, so a data migration adds the plan to existing databases, and db/seeds.rb adds it to new ones, which since Rails 8.0 load the schema instead of replaying migrations.
solid answer
~50 sA normal deploy runs `bin/rails db:migrate` (or `db:prepare` on an existing database), and neither runs `db/seeds.rb`, so a plan added only to seeds never reaches production unless someone runs `bin/rails db:seed` there. A data migration runs exactly once per database, in order with the schema changes, and is recorded in `schema_migrations`, so it is the reliable way to add the plan to production. But since Rails 8.0, `db:migrate` on a fresh database loads `db/schema.rb` first and marks every migration as run, and the schema holds no rows, so a teammate's new database never sees that migration's insert. Hence the usual answer: put the plan in the idempotent seeds file as the canonical list, and ship a data migration (or an explicit seeds run) for databases that already exist. One-off corrections that are not reference data, like fixing a few mislabelled learners, belong in a task run once.
code
ruby · 7 lines# db/seeds.rb: the canonical list for every new database
{ "free" => 0, "pro" => 999, "family" => 1999 }.each do |code, cents|
Plan.find_or_create_by!(code: code) do |plan|
plan.name = code.capitalize
plan.monthly_price_cents = cents
end
endgo deeper
Know that a deploy runs migrations, not seeds, so adding a row to db/seeds.rb alone does not create it in production.
Compare the three places: seeds run on new databases, migrations once per existing database, tasks only when someone runs them.
Explain the Rails 8 schema-load behaviour that skips old data migrations on fresh databases, and pair seeds with an idempotent migration for reference data.
Set the team rule for reference data, operational fixes and admin-editable values, including who may run tasks against production and how it is recorded.
## The three places data changes can live The language-learning app launches a **Family plan**. Billing code will look it up with `Plan.find_by!(code: "family")`, so every environment must have the row before that code runs. Rails gives three places to put the change: | | `db/seeds.rb` | data migration in `db/migrate` | one-off task (`lib/tasks`, `bin/rails runner`) | |---|---|---|---| | runs on a normal deploy | no | **yes**, via `db:migrate` | only if someone runs it | | runs on a brand-new database | **yes**, via `db:setup` or `db:prepare` | no, the schema is loaded instead | no | | runs how often | whenever invoked, so must be idempotent | once per database, recorded in `schema_migrations` | whenever invoked | | ordered with schema changes | no | yes | no | No single column says yes everywhere, which is why the answer is usually a combination. ## Why seeds alone are not enough The deploy path for a Rails 8.1 app runs migrations: the generated Docker entrypoint calls `db:prepare`, which on an **existing** database only runs pending migrations; a pipeline might call `bin/rails db:migrate` directly. Neither loads `db/seeds.rb` once the database has tables. A Family plan added only to the seeds file therefore appears in every new development database but **not in production**, and the billing code fails there. You can make seeds part of the release by running `bin/rails db:seed` after migrating, but only if the whole file is safe to run in production: idempotent, no demo data, nothing that overwrites values an admin has since edited. ## Why a data migration alone is not enough A data migration is the reliable way to change **existing** databases: ```ruby class AddFamilyPlan < ActiveRecord::Migration[8.1] def up execute <<~SQL INSERT INTO plans (code, name, monthly_price_cents, created_at, updated_at) VALUES ('family', 'Family', 1999, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) ON CONFLICT (code) DO NOTHING SQL end def down execute "DELETE FROM plans WHERE code = 'family'" end end ``` It runs once per database, in order with the schema change that may have added `plans.code`, and its version lands in `schema_migrations`. But since **Rails 8.0, `db:migrate` on a fresh database loads the schema file first**, then runs only migrations newer than the schema's version. `db/schema.rb` describes tables, not rows, so a teammate who sets up after this migration was merged gets a `plans` table **without** the Family plan. The same happens with `db:setup`, `db:reset` and `db:prepare` on a new database. Data that every database must contain therefore has to be in the seeds file too. ## The usual answer 1. Add the plan to `db/seeds.rb` with `find_or_create_by!(code: "family")`, so every **new** database gets it. 2. Ship a data migration with the same idempotent insert, so every **existing** database gets it on deploy. 3. Deploy the code that depends on the plan in the same release or later; the migration runs before the new code serves traffic. Some teams replace step 2 with a release step that runs `bin/rails db:seed`; that works when the seeds file is production-safe end to end. ## A release checklist 1. Add the row to `db/seeds.rb`, keyed on a stable code, so new databases get it. 2. Add an idempotent data migration so existing databases get it on deploy. 3. Run both locally: `bin/rails db:migrate` on your current database, and `bin/rails db:seed:replant` to prove the seeds still load. 4. Ship the code that depends on the row in the same release or after it, never before. 5. After the deploy, check the row exists in production before turning on anything that depends on it. ## Where one-off tasks fit Not every data change is reference data. Moving forty learners who signed up with a mislabelled country, or recomputing streaks after a bug, is a **one-off fix**: it should run once against production, may take a while, and has no place in new databases. That belongs in a Rake task or a `bin/rails runner` script you run deliberately, with logging, not in seeds and usually not in a migration that blocks the deploy. A last caution about data migrations: model classes change over time, and a migration that calls today's `Plan` model may break when someone replays it after the model has moved on. SQL, or a minimal model class defined inside the migration, keeps it stable.
- Why not just run bin/rails db:seed on every deploy?It works only if the whole file is production-safe: idempotent, free of demo data, and not overwriting values that admins change in production. Many seeds files are written for development comfort, and running them on every deploy turns a convenience file into a production migration path without review.
- The plan's price must change from 1999 to 2499 in production. Seeds, migration or task?A data migration (or a deliberate task) updates existing rows, because `find_or_create_by!` in seeds will not touch a row that already exists. Update the seeds file too so new databases start with 2499.
- db:migrate runs db/seeds.rb at the end of every deploy
- a data migration is enough, every database replays it
- seeds are only for development, production data goes nowhere special
- put one-off fixes for a few users in db/seeds.rb
- changing the block in seeds updates the price everywhere
Assumes Rails 8.1; since Rails 8.0 db:migrate on a fresh database loads the schema file first, so data migrations already covered by the schema version never run there.