Codebeerstartups

Spliting Seed files into multiple file in Ruby on Rails

Introduction

user

Mohit Jain

Rails Developer


ruby-on-rails

Spliting Seed files into multiple file in Ruby on Rails

Posted by Mohit Jain on .
Featured

ruby-on-rails

Spliting Seed files into multiple file in Ruby on Rails

Posted by Mohit Jain on .

Last month I joined a new job after a break of almost 4 months. You can checkout the product and there we had a small problem that our seed file was growing very fast. So we did a small thing to maintain our seed file. Here is a small tip if you are having a massive seed file and its pretty easy to implement.

We can 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 can sort the files alphabetically before loading them.

What if seed file ordering is important?

In that case we can use the trick migrations are using. Timestamp in front of file names to make sure there ordering is same. So instead of using timestamp you can add a serial number in front of file name like 01_grades.rb, 02_topics.rb, 03_skills.rb.

I hope this will help someone in case you are facing same issues.

user

Mohit Jain

http://codebeerstartups.com

Rails Developer