We need to add more seed data for some newly added tables to "version 100" of our rails project.
However, if we simply add it to the seeds.rb and re-run the rake db:seed command, it will of course Re-add the original seed data, duplicating it.
So if you’ve already added seed data to seeds.rb for, say, TableOne …
How can we incrementally add seed data for TableTwo and TableThree at later stages of development?
I’d hoped I could simply create a NEW seeds_two.rb file and run rake db:seeds_two but that gave an error Don't know how to build task 'db:seeds_two'
So it looks like ONLY "seeds.rb" can be used.
How do people maintain incremental additions to seed data?
You can re-use the
seedtask, but make it idempotent.To make the seed idempotent, simply check for the existence of the condition before executing a command. An example: do you want to create a new admin user?
instead of
However,
seedshould be used to populate your database when the project is created. If you want to perform complex data seeding durin the lifecycle of the app, simply create a new rake task, execute it then remove it.