简体   繁体   中英

What is the best practice for updating rails seed data

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?

Stand on the Shoulders of Giants

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: 'jon@example.com',   name: 'Jon'   },
  { id: 2, login: 'emily', email: 'emily@example.com', 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.

I use a clunky workaround in my seed file to stop the same data being added twice.

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

I suppose this could be modified to the following

   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

If you think there should be a rake task for something, you might want to check the available tasks.

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

That being said seed_fu provides much better functionality IMHO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM