Skip to content

merbjedi/merb_seed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

merb_seed
=======

merb_seed 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.


Support
=======

merb_seed 0.1 currently only supports datamapper. ActiveRecord and Sequel support coming soon


Usage
=======

rake db:seed


Setting up Seed Data
=======
Seed data is taken from the schema/data directory. Simply make descriptive .rb
files in that directory (they can be named anything, but users.rb for the User
model seed data makes sense, etc.). These scripts will be run whenever the
db:seed rake task is called. You can put arbitrary Ruby code in these files,
but remember that it will be executed every time the rake task is called, so
it needs to be runnable multiple times on the same database.

You can also have environment-specific seed data placed in 
schema/data/ENVIRONMENT that will only be loaded if that is the current
environment.

Let's say we want to populate a few default users. In schema/data/users.rb we
write the following code:

    User.seed(:login, :email) do |s|
      s.login = "bob"
      s.email = "bob@bobson.com"
      s.first_name = "Bob"
      s.last_name = "Bobson"
    end
    
    User.seed(:login, :email) do |s|
      s.login = "bob"
      s.email = "bob@stevenson.com"
      s.first_name = "Bob"
      s.last_name = "Stevenson"
    end

That's all you have to do! You will now have two users created in the system
and you can change their first and last names in the users.rb file and it will
be updated as such.

The arguments passed to the <ActiveRecord>.seed method are the constraining
attributes: these must remain unchanged between db:seed calls to avoid data
duplication. By default, seed data will constrain to the id like so:

    Category.seed do |s|
      s.id = 1
      s.name = "Buttons"
    end
    
    Category.seed do |s|
      s.id = 2
      s.name = "Bobbins"
      s.parent_id = 1
    end
    
Note that any constraints that are passed in must be present in the subsequent
seed data setting.


Callbacks
=======

Seed data can also define callbacks that will run after the record is created

Category.seed do |s|
  s.id = 2
  s.name = "Bobbins"
  s.parent_id = 1
  
  # run code after creation (record is the newly created model)
  s.after_seed do |record|
    record.enable! 
  end
end

Source Code
=======

Need a change? Help out by forking and doing a pull request. I'll merge in any new features or bug fixes that you make.
http://github.com/merbjedi/merb_seed


Seed Fu
=======
merb_seed is a port of seed-fu to Merb

http://github.com/mbleigh/seed-fu

About

Merb plugin that provides RAKE tasks to easily initialize seed data in your database

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages