What is the best practice for updating rail seed data

I have a rails application in production with seed data. We need to add more seed data, but with rake db:populate all old seed data will be replicated and, of course, we don’t want to add data to the migration.

What is the best way to add additional seed data to an application?

+6
source share
4 answers

You probably have to build another rake task. Or you can just do checks on each new line to see if it already exists. This may take longer, but at least you will not have duplicates.

+1
source

I am using an inept workaround in my seed file to add the same data twice.

 if Therapy.count == 0 therapies = Therapy.create([ { :name => 'Peritoneal dialysis' }, { :name => 'Haemodialysis' }, { :name => 'Plasma therapy' }, { :name => 'Laparotomy' }, { :name => 'Haemofiltration' } ]) end 

I guess this could be changed until next

  if Therapy.count == 0 therapies = Therapy.create([ { :name => 'Peritoneal dialysis' }, { :name => 'Haemodialysis' }, { :name => 'Plasma therapy' }, { :name => 'Laparotomy' }, { :name => 'Haemofiltration' } ]) elsif Therapy.count == 4 therapies = Therapy.create([ { :name => 'NEW THERAPY' } ]) end 
+1
source

Stand on the shoulders of the giants

Look at the SeedFu gem .

It allows you to create a seed file that is automatically associated with one or more columns:

 User.seed(:id, { id: 1, login: 'jon', email: ' jon@example.com ', name: 'Jon' }, { id: 2, login: 'emily', email: ' emily@example.com ', name: 'Emily' } ) 

You can also update these seed files and handle updating DB values.

This, combined with Seedbank , is what I used.

+1
source

If you think that for some reason there should be a rake task, you can check the available tasks.

 rake -T seed rake db:seed # Load the seed data from db/seeds.rb 

What is called seed_fu provides much better IMHO functionality

-1
source

Source: https://habr.com/ru/post/904281/


All Articles