This post first appeared on the Panter Blog:
Database seeds are a very handy tool in rails.
They ease setting up a proper development environment with meaningful data.
One issue with the out of the box seeds is though that they are not
idempotent per se.
While developing an application you don’t want to always reset the database
to get a proper seed state. If the seeds were idempotent,
you could run
rake db:seed as many time as you’d like without
corrupting the integrity of your data. Just as migrations,
running the seeds should only update your database if necessary.
A simple helper method helps creating idempotent data easily.
modelis the model class
find_or_create_byis a hash of attributes that are used for finding a record. If the record doesn’t exist, it will be created. This hash is like the unique identifier of a seed record.
update_withis a hash of attributes that will be always set on the record, whether the record already exists or has to be created. This is useful when the seeds are extended and you want to update existing records with new attributes.
The actual seed could look something like this.
That’s it. Now execute
rake db:seed a few times to get back your confidence in your seeds.