Seedbank gives you a convention for splitting off seeds into multiple files, it allows you to have seeds for different environments, and it gives you some additional rake tasks so you can load just one seed file if you need it , so you can generate a bunch of test data for your development database but not have it pollute your production database. Since the Rails core team were good enough to give us rake db:setup it would be rude not to use it.
$ rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)To achieve this slothful aim Seedbank renames the original db:seed rake task to db:seed:original, makes it a dependency for all the Seedbank seeds and adds a new db:seed task that loads all the common seeds in db/seeds plus all the seeds for the current Rails environment.
Example of the structure :
db/seeds/ # create a new folder 'seeds' articles.seeds.rb # create a new seed file 'articles.seeds.rb' in 'seeds' folder development/ # create a new folder 'development' users.seeds.rb # create a new seed file 'users.seeds.rb' in 'development' folder comments.seeds.rb # create a new seed file 'comments.seeds.rb' in 'seeds' folderThis would generate the following Rake tasks :
$ rake db:seed # Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/ENVIRONMENT/*.seeds.rb. ENVIRONMENT is the current environment in Rails.env. $ rake db:seed:bar # Load the seed data from db/seeds/bar.seeds.rb $ rake db:seed:common # Load the seed data from db/seeds.rb and db/seeds/*.seeds.rb. $ rake db:seed:development # Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/development/*.seeds.rb. $ rake db:seed:development:users # Load the seed data from db/seeds/development/users.seeds.rb $ rake db:seed:foo # Load the seed data from db/seeds/foo.seeds.rb $ rake db:seed:original # Load the seed data from db/seeds.rbTherefor assuming RAILS_ENV is not set or is 'development' :
$ rake db:seed # would load the seeds in db/seeds.rb, db/seeds/bar.seeds.rb, db/seeds/foo.seeds/rb and db/seeds/development/users.seeds.rb. # Whereas: $ RAILS_ENV=production db:seed # would load the seeds in db/seeds.rb, db/seeds/bar.seeds.rb and db/seeds/foo.seeds/rb
Rails 3.xAdd the seedbank gem to your Gemfile. In Gemfile:
run bundle installThat's it !!
Rails 2.xAdd to your config/environment.rb
config.gem 'seedbank'Install the gem;
$ rake gems:installThen in the bottom of your application's Rakefile:
require 'seedbank' Seedbank.load_tasks if defined?(Seedbank)If you vendor the gem you'll need to change the require to the specific path.
Usage :Seeds files are just plain old Ruby executed in your rails application environment so anything you could type into the rails console will work in your seeds.
User.find_or_create_by_name('John', :email => 'email@example.com' )
The seed files under db/seeds are run first in alphanumeric order followed by the ones in the db/seeds/RAILS_ENV. You can add dependencies to your seed files to enforce the run order. for example :db/seeds/articles.seeds.rb :
after :users do user = User.find_by_name('John') user.articles.create(:title => 'Ruby on Rails', :content => 'this is a content') enddb/seeds/comments.seeds.rb :
after :articles do article = Article.find_by_title('Ruby on Rails') article.comments.create(:content => 'Good') end
If the dependencies are in one of the environment folders, you need to namespace the parent task:db/seeds/development/articles.seeds.rb
after "development:users" do user = User.find_by_name('John') user.articles.create(:title => 'Ruby on Rails', :content => 'this is a content') end
Seedbank allows you to structure your Rails seed data instead of having it all dumped into one large file so you can generate a bunch of test data for your development database but not have it pollute your production database.