Seeding your Database with Rails Seeds and The Faker Gem

Sunday Nov 26, 2017 Kudzai Nyandoro


In this tutorial, we are going to create a tiny rails application and explore several ways in which we can populate our database.
We will explore what Rails gives us out of the box as well as the Faker gem. If you would like to follow along and you do not Ruby on Rails installed please visit the InstallRails website, install Ruby on Rails and return to this page to follow along.

Lets say we want to build a writing application which we will name journal. Our journal app is going to allow us to create a title and enter some text in the body of the journal. After creating the journal's model we want to be able to add data to our database so that our client can play around and experiment with the app. We also want to give other developers who might be working on our app, the ability to quickly add data to our database when they clone our app.

Creating the Journal Application
  • Please note I'm using Rails 5.1.4 and Ruby 2.4.2. If your are using Rails 4x or less, replcase 'rails' with 'rake' when running the rails commands.
  • Lets hope into our terminal and create our journal application by running the line provided below
$ rails new journal-1
  • Next we are going to use the Rails scaffold generator to add a feature that will give our client the ability to add a journal entry with a title and a body. Lets run the line provided below.
$ rails generate scaffold Post title:string body:text
  • If you look at your terminal you should notice that rails created a bunch files. With this scaffold generator we now have a Posts controller, Post Model, Post index and show pages etc, as shown below.
invoke  active_record
      create    db/migrate/20171126150013_create_posts.rb
      create    app/models/post.rb
      invoke  resource_route
       route    resources :posts
      invoke  scaffold_controller
      create    app/controllers/posts_controller.rb
      invoke    erb
      create      app/views/posts
      create      app/views/posts/index.html.erb
      create      app/views/posts/edit.html.erb
      create      app/views/posts/show.html.erb
      create      app/views/posts/new.html.erb
      create      app/views/posts/_form.html.erb
      invoke    helper
      create      app/helpers/posts_helper.rb
      invoke    jbuilder
      create      app/views/posts/index.json.jbuilder
      create      app/views/posts/show.json.jbuilder
      create      app/views/posts/_post.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/
      invoke    scss
      create      app/assets/stylesheets/posts.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss
  • Next we need to run a migration so that Rails can create our Posts table using the following line.
$ rails db:migrate
  • Now let us open up the config/routes file and add the root route as shown below.
  Rails.application.routes.draw do
    root  'posts#index'
    resources :posts
  • If you go to your browser and visit localhost:3000 you will notice that you do not have any data to view, however, you could click on the 'New Post' link and add the data one at a time. You can also enter into your Rails console and add the data like so ...
$ rails console

> Post.connection

> Post.create(title: "First Journal", body: "Today I had fabulous day!")
  • However, Rails provides a 'seeds' feature that comes baked into it, and it makes our lives much easier when it comes to adding new data to your application. Use your favorite text editor to open up the 'seeds.rb' file located in db/seeds.rb and add the following line
  Post.create(title: "First Journal", body: "Today I had a fabulous day!")
  • Notice this is the same line of code we ran in our Rails console, only this time we have it saved in our seeds.rb file. This means we do not have to type it in again if we ever reset our database.

  • If you wanted to create several journal entries at once you could replace the above code with the following in your db/seeds.rb file.

 10.times do |post|
    Post.create(title: "First Journal", body: "Today I had a fabulous day!")
Using Faker Gem
  • You could, instead, use Faker gem to seed your database, first, you need to install the Faker gem. Open up your Gemfile and add the following gem.
gem 'faker', '~> 1.8', '>= 1.8.4'
  • Run the following line to install the gem
$ bundle install
  • Our next step is to open up our 'seeds.rb' file located in 'db/seeds.rb', and replace the code with the one provided below.
  10.times do 
    title = Faker::Lorem.sentences(1)
    body = Faker::Lorem.paragraph(10)
    Post.create!(title: title, body: body)

  puts "***************** Your seeds have been planted! ********************"
  • The code above is going to use Faker gem to create 10 posts (our journal entries) as soon as we seed our database as shown below.

  • First let us make sure that all our migrations are up by running the following line.

$ rails db:migrate:status
  • Now look at your terminal and make sure that your migration says 'up' as shown below.
 Status   Migration ID    Migration Name
   up     20171126150013  Create posts
  • If your migration status says 'down' run the following line
$ rails db:migrate
  • Now let seed our database
$ rails db:seed
  • If you fire up your browser and visit 'localhost:3000' you should see 10 new entries were created on your landing page.

  • To learn more about seeding visit the Rails Guides , to learn more about the Faker gem visit Faker gem on Github.

  • To clone this repository visit my Github page here