Sign in
Log inSign up
Seeding Your Database in Rails - Part 1

Seeding Your Database in Rails - Part 1

Mike Chilson's photo
Mike Chilson
·Jul 26, 2018

New Rails developers are sometimes overwhelmed with all the cool features Rails offers. The seeds.rb file this is one feature you want to know about for sure (if you don't already). I originally published this post about a year ago on my blog in hopes it would help new Rails developers understand the basics of this file. This is part 1 of 2 explaining the "how and why" of the seeds.rb file. In part 2 we will cover how to use the seeds.rb file in a practical manner. Hope someone out there finds it useful. ~Mike

When writing applications in Rails that use databases (as most do), we often have the need to populate small "reference" tables with data (like credit card types, colors, dog breeds, categories, etc) used in form drop-downs, test data when developing or inserting specific records into the database at deployment time. This is accomplished in Rails with the seeds.rb file located in the DB directory of your rails project folder. Surprisingly, most new Rails developers do not know about this file. If you open the file seeds.rb in Rails 5.x you will see some examples of how you insert records. Here is what you will see:

# This file should contain all the record creation needed to seed 
# the database with its default values.
# The data can then be loaded with the rails db:seed command 
# (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)

So to use this file, you simply create the records individually like this in the seeds.rb file:

CreditCard.create("name"=>"Visa", "image"=>"/imagepath/visa.png")
CreditCard.create("name"=>"Mastercard", "image"=>"/imagepath/mastercard.png")
CreditCard.create("name"=>"American Express", "/imagepath/image"=>"amx.png")

As you can see there is nothing magical about the seeds.rb file. It's just a ruby file that you can run code to seed the database just like you can in the Rails Console. Another useful feature of the seeds.rb file is you can use it to populate test data during developing. Here is a simple example of adding some test data to my skills model using a block:

5.times do |skill|
  Skill.create!(
    title: "Rails #{skill}",
    percent_utilized: 20
  )
end
puts "Skills records seeded"

See, nothing to it!

One note here. If you are going to upload your code to a public repository (like GitHub or Bitbucket) do not add live user account information to your seeds.rb file

Once you get your seeds.rb file built you can populate the data using the command:

rails db:seed

If you want to learn more about the seeds.rb file. Here are some references, articles, and tools you can check out.

Active Record Migrations

Seeding a database using the Rails command line

Faker Gem

Populator Gem

We will dig deeper into the seeds.rb file in part 2. Happy Coding!