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.
- Please note I’m using Rails 5.1.4 and Ruby 2.4.2. If your are using Rails 4x or less, replcase
rakewhen 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.
- 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.
- 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
- 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
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.
- 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.
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
- If your migration status says ‘down’ run the following line
$ rails db:migrate
- Now let seed our database
$ rails db:seed