Rails recommended way to add sample data
You should not fill your database with sample data via db:seed
. That's not the purpose of the seeds file.
db:seed
is for initial data that your app needs in order to function. It's not for testing and/or development purposes.
What I do is to have one task that populates sample data and another task that drops the database, creates it, migrates, seeds and populates. The cool thing is that it's composed of other tasks, so you don't have to duplicate code anywhere:
# lib/tasks/sample_data.rakenamespace :db do desc 'Drop, create, migrate, seed and populate sample data' task prepare: [:drop, :create, "schema:load", :seed, :populate_sample_data] do puts 'Ready to go!' end desc 'Populates the database with sample data' task populate_sample_data: :environment do 10.times { User.create!(email: Faker::Internet.email) } endend
I would suggest making rake db:seed
self sufficient. By which I mean, you should be able to run it multiple times without it doing any damage, while ensuring that whatever sample data you need loaded gets loaded.
So, for your researches, the db:seed task should do something like this:
User.destroy_all10.times do researcher = User.new researcher.email = Faker::Internet.email researcher.save!end
You can run this over and over and over and are ensured you will always end up with 10 random users.
I see this is for development. In that case, I wouldn't put it in db:seed as that might get run in production. But you can put it in a similar rake task that you can re-run as often as needed.