Handling large seed files in Ruby on Rails
Posted on October 18th, 2011 in Default | 1 Comment »
Sometimes seed files can get messy and big. It can be real pain it the ass to manage them. Here is fast way to split single seeds.rb file:
- Create directory called seeds in your db/ directory (mkdir ./db/seeds)
- Remove all stuff from seeds.rb and move it into your newly created files under db/seeds/ directory (put them accordingly to your own app logic)
- Paste code presented below into seeds.rb file
- Run rake db:seed
Seeds.rb file source code:
# coding: utf-8
%w{
filename1 filename2 filename3...filenameN
}.each do |part|
require File.expand_path(File.dirname(__FILE__))+"/seeds/#{part}.rb"
end
Why haven’t I use auto-include and instead I’ve listed all the files? Well I wanted to maintain my seed parts load order so those parts will be loaded accordingly to my order (not based on file names).
One Response
Thank you very much for this small, elegant and usefull solution!