Search code examples
ruby-on-railspostgresqlhas-and-belongs-to-manyseed

Preserving join table data when reseeding a database


I'm reseeding a fairly large database with updated static data and updated values which users engage with. However, I've noticed that reseeding the database clears all join tables for Has_and_belongs_to_many tables (all other user data, and tables remains unchanged).

Has anyone worked out a way around this where the data will be preserved when reseeding?

I'm using Rails & Postgresql


Solution

  • As mentioned above you will need to recreate the associations. You can do this fairly straight forward in your seed file.

    You might also look into the seed dump gem. This will allow you to snapshot your HABTM relationships and then export them into your db/seeds.rb (or whatever) file.