# \[ANN\] Seed -- a seed data/fuzzing plugin

**URL:** <https://www.ruby-forum.com/t/ann-seed-a-seed-data-fuzzing-plugin/133582>\
**Category:** Rails\
**Created:** [April 1, 2008, 9:27pm UTC](https://www.ruby-forum.com/t/ann-seed-a-seed-data-fuzzing-plugin/133582 "2008-04-01T21:27:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_McAnally](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c89c15/32.png) [@Jeremy\_McAnally](https://www.ruby-forum.com/u/Jeremy_McAnally)\
**Post date:** [April 1, 2008, 9:27pm UTC](https://www.ruby-forum.com/t/ann-seed-a-seed-data-fuzzing-plugin/133582/1 "2008-04-01T21:27:36Z")

</div>

Seed is a plugin to help with generating and/or inserting seed data  
into your database. Seed will  
generate data for you or load it from fixtures or arbitrary Yaml, CSV,  
or (eventually) XML  
files. The basic workflow goes like this:

1. Create seed files. These live in db/seeds/ and are named for the  
tables of your models,  
like +Person+ (model), +people+ (table) would yield +people\_seed.rb+.

2. Create a class named for the file (+people\_seed.rb+ would have  
+PeopleSeed+) that inherits from  
+Seed::Planter+.

3. Fill in your directives for attributes or file loads, or you can  
leave it empty and Seed will  
enumerate your columns and generate data for you.

4. Run rake db:seed to seed your database.

You can also generate static versions of the seeds for your current  
models using the db:seeds:generate  
Rake task.

So, let’s say you have a model +User+, with attributes +username+  
(string), +password+ (string),  
+email\_address+ (string), favorite\_quote (string), and administrator  
(boolean). To get a basic seed  
file, you need to execute rake db:seeds:generate. This task  
will yield a file like this:

```
class UsersSeed < Seed::Planter
  attribute :username, :string
  attribute :password, :string
  attribute :email_address, :string
  attribute :favorite_quote, :string
  attribute :adminstrator, :boolean
end

```

Now, since we have a few special fields, we can use Seed’s special  
types for +username+, +password+, and +email\_address+.

```
class UsersSeed < Seed::Planter
  attribute :username, :username
  attribute :password, :password
  attribute :email_address, :email
  attribute :favorite_quote, :string
  attribute :adminstrator, :boolean
end

```

This will generate proper data (for example, “jmcanally” rather than  
“Lorem ispum dolormet” for username).

Run rake db:seed and voila! A seeded database!

You can also skip specifying a type and provide the data for the field  
in a +Proc+. For example, if you wanted to  
have some specialized logic for setting the value of a field:

```
class JobsSeed < Seed::Planter
  attribute :description, :string
  attribute :script, Proc { ['upload', 'update',

```

‘find\_files’].sort { rand(3) - 1 }[0] }  
end

You can also call methods from the seed class in the +Proc+ if you’d  
like (direct support for method class on the  
seed class will be added soon).

# Examples

```
# db/seeds/products_seed.rb
class ProductsSeed < Seed::Planter
  attribute :name, :string

  # Address attribute types are special and generate
  # proper data.
  attribute :address, :address

  attribute :email, proc { "removed_email_address@domain.invalid" }
end

# db/seeds/people_seed.rb
class PeopleSeed < Seed::Planter
  # Tells Seed that you want to load seed data from
  # the file db/seeds/people.yml
  loads_from "people.yml"
end

# db/seeds/things_seed.rb
class ThingsSeed < Seed::Planter
  # Tells Seed that you want to load seed data from
  # the fixtures file (test/fixtures/things.yml)
  loads_from :fixtures
end

```

This point of the plugin is to let you easily seed data/have seed data  
generated  
for you to get your app up and going quickly. Nothing is more  
irritating than having  
to spend time in script/console or YourSQL to create some base data to  
work from.

It’s on GitHub at [http://www.github.com/jeremymcanally/seed](http://www.github.com/jeremymcanally/seed) or you can  
clone from  
here: git://github.com/jeremymcanally/seed.git .

Pull requests, suggestions, and bug reports are appreciated.

–Jeremy

–  
[http://jeremymcanally.com/](http://jeremymcanally.com/)  
[http://entp.com](http://entp.com)

Read my books:  
Ruby in Practice ([Ruby in Practice](http://manning.com/mcanally/))  
My free Ruby e-book ([http://humblelittlerubybook.com/](http://humblelittlerubybook.com/))

Or, my blogs:

> **[Site not found · DreamHost](http://mrneighborly.com)**
>
> The owner of this domain has not yet uploaded their website.

[http://rubyinpractice.com](http://rubyinpractice.com)
