README.md

Seed Fu

Seed Fu is an attempt to once and for all solve the problem of inserting and maintaining seed data in a database. It uses a variety of techniques gathered from various places around the web and combines them to create what is hopefully the most robust seed data system around.

Warning: API Changes

Version 2.0.0 of Seed Fu introduced API changes. Seed::Writer was been completely overhauled and will require you to update your scripts. Some other deprecations were introduced, and support is fully removed in version 2.1.0. Please see the CHANGELOG for details.

Installation

Rails 3.1

Seed Fu depends on Active Record, but doesn't have to be used with a full Rails app. Simply load and require the seed-fu gem and you're set.

Rails 3.0

The current version is not backwards compatible with Rails 3.0. Please use gem 'seed-fu', '~> 2.0.0'.

Rails 2.3

The current version is not backwards compatible with Rails 2.3. Please use gem 'seed-fu', '~> 1.2.0'.

Constraints

Constraints are used to identify seeds, so that they can be updated if necessary. For example:

Point.seed(:x, :y) do |s|
s.x = 4
s.y = 7
s.name = "Home"
end

The first time this seed is loaded, a Point record will be created. Now suppose the name is changed:

Point.seed(:x, :y) do |s|
s.x = 4
s.y = 7
s.name = "Work"
end

When this is run, Seed Fu will look for a Point based on the :x and :y constraints provided. It will see that a matching Point already exists and so update its attributes rather than create a new record.

If you do not want seeds to be updated after they have been created, use seed_once:

Point.seed_once(:x, :y) do |s|
s.x = 4
s.y = 7
s.name = "Home"
end

The default constraint just checks the id of the record.

Where to put seed files

By default, seed files are looked for in the following locations:

Rails.root/db/fixtures and Rails.root/db/fixtures/Rails.env in a Rails app

db/fixtures when loaded without Rails

You can change these defaults by modifying the SeedFu.fixture_paths array.

Seed files can be named whatever you like, and are loaded in alphabetical order.

Terser syntax

When loading lots of records, the above block-based syntax can be quite verbose. You can use the following instead: