Split seeds.rb file [duplicate] Split seeds.rb file [duplicate] ruby-on-rails ruby-on-rails

Split seeds.rb file [duplicate]


We store all our seeds inside the folder db/seeds and inside the db/seeds.rb we write the following:

Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].sort.each { |seed| load seed }

We sort the files alphabetically before loading them, so we can make sure the files are processed in order (by choosing the filenames wisely, e.g. something like 01_operators.rb, 02_companies.rb, 03_products.rb ...).

Hope this helps.


Since your seeds.rb file is Ruby, then you can do anything in it you can do in Ruby. Like routes.rb and Gemfile you can get quite creative if required.

Just be sure that you don't do anything so crazy it would actually require some debugging. As long as you keep your actions clear, there should be no confusion. That is, don't use custom helper methods that are defined in some other file that could malfunction and would have to be tracked down by hand. It's probably best to stick to the simplest solution when doing things like this.