‘seed’ data versus ‘testing’ data (and custom rake tasks) for Ruby on Rails

I’ve been a part of discussions on how to define ‘seed’ data versus ‘testing’ data on a Ruby on Rails project. Here’s a summary of what I’ve seen work well — please comment if you have anything to add.

Data loading is done for 2 purposes: First, to load data that will seed the system for ongoing use and that will live in the production environment, and second, to use for system testing. These two types of data are stored in two different ways.

Loading ‘seed’ data that will be required in production.

This data should be defined in /db/seeds.rb and loaded using the rake db:seed command. It should be loaded one time after initial database creation. If changes to the seeds.rb file are made, the database should be rebuilt and reloaded from scratch.

Loading test data.

This is data that is required for testing the application. Specifically, it is data that should not appear in the production application.

This data should be loaded through model classes (or using factory_girl or similar) in a ‘rake’ file, for example /lib/tasks/test_data.rake and then load it using the rake task rake app:load_demo_data. Similar to loading seeds.rb, if the test data changes then the database should be reloaded from scratch.

(Note that here the rake file is named /lib/tasks/test_data.rake and the rake task to load the data is rake app:load_demo_data. This would vary based on what you felt was an appropriate name for your project.)

Overall database loading/unloading

The correct way to load data into the database is to use the following rake commands:

rake db:drop db:create db:migrate db:seed app:load_demo_data

As you see, multiple rake commands can be run one after another sequenced on the same line.

This should be able to be done at any time by any person in development. If you make a change to any data, please assume that the above sequence of rake commands could be run at any time and all data could be replaced.

Note also that the above command will load data into the ‘development’ instance of your database. If you need to load (or reload) the production or test environments, then precede this command with the appropriate RAILS_ENV like so:

RAILS_ENV=test rake db:drop db:create db:migrate db:seed app:load_demo_data

Here’s an example of what the rake file /lib/tasks/test_data.rake might look like:

namespace :app do
  desc <<-DESC
    Load testing data.
    Run using the command 'rake app:load_demo_data'
  task :load_demo_data => [:environment] do

  # Only data not required in production should be here.
  # If it needs to be there in production, it belongs in seeds.rb.


  User.create(:login => "test_user", :password => "testpass")
  Category.create(:category => 'Category #1')

  # Other test data should be added here...


This entry was posted in agile, programming, rails, ruby on rails and tagged , , , , . Bookmark the permalink.
  • http://www.allerin.com/ Ruby on Rails

    Thanks for the explanation by giving the proper example.

    Current score: 0
  • bassline

    Thank you. Worked perfectly on rails 3.1.1.

    Current score: 0
  • Mikeorr85

    Awesome.  Just what I was looking for.

    Current score: 0
  • Murty

    Excellent!  I had to add   ActionMailer::Base.default_url_options = {host: “localhost:3000″}
    to make it work.

    Current score: 0