Jeremy Durham

Ruby, Rails, and everything in between.

  • BlogThoughts
  • AboutAbout the Author
  • ContactGet in touch

May 05, 2010 00:31
Posted by Jeremy Durham

New gem - Seed

I pushed a new repository to Github and a new gem to Gemcutter. It’s called Seed.

Seed is (another) gem designed to deal with seeding databases.

The main advantage of using Seed is that when creating a seed, or retrieving a seed, an error is throw if you attempt to overwrite a seed or retrieve a non-existent seed. The goal is to make you care about your seed data, and ensure that you’re not overwriting existing seeds or trying to make an association between a seed and a non-existent seed.

How does it work?

Role.seed(:system_administrator, :role => 'system_administrator')
User.seed(:jdurham, :login => 'jdurham', :roles => [Role.seed(:system_administrator])

if you like blocks, we’ve also got those covered:

Role.seed(:system_administrator) do |role|
  role.role = 'system_administrator'
end
User.seed(:jdurham) do |user|
  user.login = 'jdurham'
  user.roles = [Role.seed(:system_administrator)]
end

This will create a Role seed, named “system_administrator”, which we will reused in other seeds. That way, when looking at another seed, it’s very obvious what associated data it has.

Overplanting not allowed

Later on, if I decide I want to create a new seed and I do:

Role.seed(:system_administrator, :role => 'admin')

This will raise an exception, because there will be an ambiguity issue. I’m trying to create another seed named system_administrator.

Trying to get a seed that never existed

User.seed(:normal_user, :roles => [Role.seed(:user)])

This will raise an exception because it doesn’t make sense to try to associate a user with a seed that doesn’t exist.

Great, I’ve got seed data. Now, how do I load it?

Seed comes with a rake task that extends the built-in rake db:seed functionality. In your Rakefile add:

require 'seed/task'

Now, create a db/seeds directory and add a file; for example, development.rb to that directory. Once you do, you’ll automatically have a rake task that can be invoked as follows:

rake db:seed:development

Hopefully you find Seed useful. We’ve been using it in production for a little while and it’s been working well.

6 Comments

Posted Under Programming

6 Comments

D10ca8d11301c2f4993ac2279ce4b930?s=32&r=g Ben Simpson
May 5, 2010

An appropriate time of year to announce your seed gem. Looking good, but I am curious how foreign key references work when there is a validation enforced in the model?

D309592f2a210d745b8044847b214bb6?s=32&r=g Jeremy
May 7, 2010

The specs have a few examples of create seeds with validations and associations, so I would refer to those. Under the covers, seed uses .create! to generate the seed, so anything you can do with .create you can do with seed. The one edge case to this is protected attributes. In order to set attributes that are protected (attr_protected or not listed as attr_accessible) is to use the block syntax. There are also specs to explain this behavior.

4766d2e7da06bd36b6d2b948d105dee2?s=32&r=g loifvjvzxb
August 12, 2010

epzfjdvejhevylfbqgym, http://www.lyfwqhgves.com ewnlrfkxrk

25cacda0ed63bc4431a42818b263583f?s=32&r=g ruby developr
August 16, 2010

Havent read the code yet. Does Seed support loading seed data from txt or yaml files?

D309592f2a210d745b8044847b214bb6?s=32&r=g Jeremy
August 19, 2010

@ruby developr: The seed data is loaded from .rb files, similar to the built in seeds support in Rails 2.3.x. This gem is more of an extension that adds "seed groups" and some validation to prevent overwriting data and orphaned data.

F5abaaaa9e9537844e0941b7695a7836?s=32&r=g cdpjexvzvz
August 25, 2010

kpanmkicyhyxcssleqvk, http://www.ipdcomzplr.com rrcteryyme

Leave a comment

* = Required

  • Posts
  • Twitter
  • Flickr
 

Capybara + parall...

Programming

 

CI rerun

Programming

 

New gem - Seed

Programming

@scottschulthess interesting. I don't follow Rails development as closely as I used to. Thanks for the heads up.

follow me on
twitter

125942640_a3dcc3f951_s125942637_c4524c7c88_s125942628_a682f8a639_s122144184_5f2dd86929_s122144182_1b01ded312_s122144181_893a82f1d3_s122144179_70465b00ff_s122102402_21e6174af6_s122102401_f6d9ebff55_s

Categories

  • Technology
  • Programming
  • Family
  • Travel

This site is using the Handgloves WordPress Theme
Designed & Developed by George Wiscombe

Subscribe via RSS