FactoryGirl for Seed Data?

FactoryGirl for Seed Data?

Occasionally, somebody recommends or asks about using FactoryGirl to generate seed data for an application. We recommend against it for a few reasons:

  1. The default attributes of a factory may change over time. This means that, when you’re using FactoryGirl to generate data in seeds.rb, you’ll need to explicitly assign each attribute in order to ensure that the data is correct, defeating the purpose.
  2. Attributes may be added, renamed, or removed. This means your seeds file will always need to be up to date with your factories as well as your database schema. You likely won’t be running rake db:seed every time you change a migration. So, your seeds file may become out of sync and it won’t be immediately obvious. You’ll likely notice a breakage when a new developer comes onto the project.
  3. Data will still need to be checked for presence before insertion. The ActiveRecord gem gives you this with the “find or create” methods to locate a record or create it if it can’t be found. In addition, those methods will basically force you to manually define each attribute you want assigned, making Factory Girl unnecessary.

What’s next?

If you found this useful, you might also enjoy:

Josh Clayton Developer

Sharpen your programming skills by completing coding exercises that are reviewed by other developers at Upcase today.