22

My seeds.rb file is getting very large. What is the best way to refactor the data in the file?

Can I put the data into various files and require them in the seeds.rb file?

gioele
  • 9,748
  • 5
  • 55
  • 80
chief
  • 730
  • 1
  • 9
  • 19

2 Answers2

70

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.

nathanvda
  • 49,707
  • 13
  • 117
  • 139
  • 7
    Super helpful. I have looked everywhere for a nice solution to this problem. Thank you for posting. – brandontreb Dec 11 '13 at 02:19
  • 2
    And if you're writing a Rails Engine, beware to use `YourEngineName::Engine.root` instead of `Rails.root` – Hobbes Mar 10 '17 at 10:23
  • This seems easier, then you can just move lines up/down in the seeds file instead of renaming the files: https://stackoverflow.com/a/4532890/380607 – Magne Jun 08 '17 at 13:51
  • Easier? In the linked answer one has to create the file _and_ edit the seeds.rb file to load it? i am lazy, and i have a lot of seed-files. But i understand one might prefer the explicitness. If it works better for you that is awesome – nathanvda Jun 09 '17 at 21:05
  • @nathanvda Excellent idea, but naming it `01` `02` is just asking for trouble when reorganizing. Name it `0100` and `0200` and if you want to move `99` between 1 and 2 you just name it `0150` instead of having to rename `02` to `98`. If you space the numbers by X (100 in this case), and always choose the middle number, you'll have up to `log_2(X)` (6 in this case) insertions that can be made before you need to rename something to get a new one. It's been a few years since you originally posted this and hopefully you worked that out by now. – Nuclearman Jul 14 '17 at 19:55
  • 1
    "Hopefully you worked that out"???? I gave a simple example how files could be named. If in your case it occurs frequently you have to insert seeds, better naming schemes should be used. Frankly in my case it is extremely rare that i have to insert seeds when adding models. – nathanvda Jul 24 '17 at 06:03
  • Thank you so much man. You make me a better clean coder on my way :) – Travis Le Jan 15 '19 at 05:20
3

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.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 2
    Included this in the seeds.rb file: require "#{Rails.root}/db/data.rb". It seems to work without any issues. – chief Oct 31 '11 at 19:25