Seeds your ActiveRecord models from Foxy Fixture-like YAML files
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
rails
test
MIT-LICENSE
README.rdoc
Rakefile
VERSION
yaml_seeder.gemspec

README.rdoc

YamlSeeder

Seeds your ActiveRecord models from Foxy Fixture-like YAML files

YAML seed files

YamlSeeder works upon seed files which are very similar to fixtures - the foxy-fixtures introduced in Rails 2.0. For example, consider a Company model and an Employee model, where an employee belongs to a company:

companies.yml

dunder:
  name: Dunder Mifflin

employees.yml

michael:
  name: Michael Scott
  company: dunder

It's a good idea to put these seed files in their own directory, say “db/seeds”.

For more information, google “foxy fixtures” or see ryandaigle.com/articles/2007/10/26/what-s-new-in-edge-rails-fixtures-just-got-a-whole-lot-easier

Installation

gem install yaml_seeder

Usage

Use YamlSeeder to seed your Rails database. If you're using Rails 2.3.4 or later, add the following to your “db/seeds.rb” file

db/seeds.rb

Dir.glob(RAILS_ROOT + '/db/seeds/*.yml').each do |file|
  YamlSeeder.seed('db/seeds', File.basename(file, '.*'))
end

Alternatively, or if you're using an earlier version of Rails, you can create a rake task (eg. called “db:yaml_seed”):

lib/tasks/seed.rake

namespace :db do
  desc "Load seed files (from db/seeds) into the current environment's database." 
  task :yaml_seed => :environment do
    Dir.glob(RAILS_ROOT + '/db/seeds/*.yml').each do |file|
      YamlSeeder.seed('db/seeds', File.basename(file, '.*'))
    end
  end
end

Notes

* If ID's are not specified in the YAML seed files, then they will be assigned by the normal creation of saving an ActiveRecord (ie. the next ID available).
* Currently, YamlSeeder only supports belong-to associations.

TODO

  1. Add support for “has and belongs to many” associations

  2. Add test cases

Copyright © 2010 Paul Fedory, released under the MIT license