[Ruby on Rails] Separating seed_fu by Environment

Tadashi Shigeoka ·  Wed, January 30, 2013

I separated seed_fu files by development environment in Ruby on Rails.

% ls -l db 
total 64
drwxr-xr-x  11 shigeoka  staff    374  1 29 20:10 fixtures/
drwxr-xr-x   3 shigeoka  staff    102 12 27 18:16 fixtures_development/
drwxr-xr-x   3 shigeoka  staff    102  1 30 18:07 fixtures_production/
drwxr-xr-x   3 shigeoka  staff    102  1 30 18:06 fixtures_staging/
drwxr-xr-x  76 shigeoka  staff   2584  1 29 20:10 migrate/
-rw-r--r--   1 shigeoka  staff  18269  1 29 20:10 schema.rb
-rw-r--r--   1 shigeoka  staff    343 12  4 23:32 seeds.rb

Common data for all environments is placed in the regular fixtures directory.

For other data, since the data you want to import differs slightly between development, staging, etc., I created separate directories with environment names as suffixes.

To import seed data for each environment, specify FIXTURE_PATH like this:

% rake db:seed_fu FIXTURE_PATH=db/fixtures_development

For files common to development and staging, I use require to load them.

# db/fixtures_development/users.rb
require "#{Rails.root}/db/fixtures_staging/groups.rb"

Something like this.

That’s all from the Gemba.