27

I'm learning Ruby on Rails, and I'd like to just make sure I understand the difference between fixtures and seed data.

What I understand is that fixtures are basically test data that you run your testing assertions against, and have no persistence as soon as your tests are done, whereas seeds you put into the database automatically when you do something like rake db:seed.

Why use seeds in this case? Just to avoid having to write out all the myriad testing assertions? For data that you know will need to be in the database when the app is brought into production?

(I guess, static data that would always have been there like the first admin on a message board?)

mwfearnley
  • 3,303
  • 2
  • 34
  • 35
user2258651
  • 383
  • 5
  • 8

2 Answers2

21

You got it right. Seed data is to populate the database with prerequisite data needed to allow the app to be usable from the beginning, like adding a default admin account required for the app to be manageable from the start.

Seed data should not be used for testing purposes. While testing you should always make sure the database is clean so that you know that the only data used by an example is the data populated by the example itself not from anywhere outside, this allows you to avoid confusion.

Fixtures are one way of doing this. However, a better way is to use factories like factory girl, check this railscast episode for a better explanation.

Nafaa Boutefer
  • 2,169
  • 19
  • 26
  • 2
    A good answer, except I wouldn't discount fixtures so quickly. The latest versions work well. Try fixtures first. If they don't do what you want, then look for alternatives like factory girl. – ReggieB Mar 19 '15 at 08:02
  • See http://brandonhilkert.com/blog/7-reasons-why-im-sticking-with-minitest-and-fixtures-in-rails/ – ReggieB Mar 19 '15 at 08:10
9

Many apps need some data just to run properly. For example, a list of countries and zip codes. Or, a list of potential roles for users to take on, even if there are no users in the database yet.

Think of the seed data in terms of what would be needed to get a development environment up and running. Generally, for a new Rails developer to work on any app, the process should always go like this:

  1. Clone the repo
  2. Install the gems via bundler
  3. Create and seed the development and test databases
  4. Run the tests, which should pass
Dogweather
  • 15,512
  • 17
  • 62
  • 81