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).
January 4, 2012 — 10:57
Thank you very much for this small, elegant and usefull solution!
November 11, 2016 — 10:15
more elegant way Dir[File.join(Rails.root, ‘db’, ‘seeds’, ‘*.rb’)].sort.each { |seed| load seed }