When my current project started I was still learning RoR and we had a more experienced guy driving our project organization (where files go, what we do to work with the project). One of the things he asserted is that when you have seed data for tables you put that seed data into the migrations themselves. And he also asserted that db/seeds.rb was only used for setting up the test db between tests when executing units or ATs (we use rspec and cucumber respectively).
Recently I’ve been reading various answers here that imply that we’re doing this incorrectly and I’m hoping that someone here can give me a deeper explanation of what the “Rails Way” is for this.
thanks!
EDIT:
A few things that I need to understand:
a. How DO I ensure that this seeded data is in the database for tests. I’ve been told that rspec tests clean the database by putting the test in a transaction and at the end simply rolling back that transaction so if that is true, does the test database get seeded before the entire run of tests? What causes the seeding to happen.
rake db:test:prepare
doesn’t seem to seed the database, although I might be mistaken
b. do I have to actually run
rake db:migrate
rake db:seed
after a migration is introduced that requires new seed data to be added to db/seeds.rb?
c. do I have to enclose each individual part of the seeding with checks to make sure it’s necessary so that I don’t get exceptions when rerunning a seeds and having, for example, uniqueness constraints violated when re-seeding is attempted on things previously seeded. (which leads to the question, is that question even syntactically correct??).
I think that the thing that led “our guy” down the path of seeding in the migrations is that we can count on the entire migration mechanism not to rerun previous seeds rather than checking everything in seeds.rb before attempting the seed…
I hope this is clearer…
Migrations are for manipulating the structure of your database, not for the data within it and certainly not for simple population tasks.
simply this is a rake task that sucks in the data specified in a db/seeds.rb.