I have a rails application in production with seed data. We need to add more seed data, but using rake db:populate
will replicate all the old seed data and of course we don't want to add the data to migrations.
What is the best method of adding extra seed data to the application?
What is Seed in Rails ? A useful way of populating a database with the initial data needed for a Rails project. This will allow us to populate the database in within our Rails Web application.
Create a migration with rails g seed_migration NameOfMigration . That will create a new file under db/data . Write your changes in the up and down methods, exactly like you would do for a regular migration. For example, we have marked our Product , TaxRate and ShippingType models as seed data.
The seeds.rb file is where the seed data is stored, but you need to run the appropriate rake task to actually use the seed data. Using rake -T in your project directory shows information about following tasks: rake db:seed. Load the seed data from db/seeds.rb. rake db:setup.
db:reset: Resets your database using your migrations for the current environment. It does this by running the db:drop , db:create , db:migrate tasks. db:rollback: Rolls the schema back to the previous version, undoing the migration that you just ran.
Take a look at the SeedFu gem.
It allows you to create a seed file like this, that automatically links to one, or more, columns:
User.seed(:id,
{ id: 1, login: 'jon', email: '[email protected]', name: 'Jon' },
{ id: 2, login: 'emily', email: '[email protected]', name: 'Emily' }
)
You can also update these seed files and it will handle updating the DB values.
This, in combination with Seedbank, is what I ended up using.
Your probably going to have to build another rake task. Or you could just do checks on each new row to see if it already exists. It may take more time to run like that, but at least you won't have duplicates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With