Using the Faker Gem to Seed Your Database
Something that has become exceedingly clear to me as I’ve continued in my backend learning is the importance of having a populated database. It’s one thing to build out the database, but when it comes to actually testing our code, oftentimes it’s necessary to have a populated database to help ensure our backend code is working the way we want it to when we perform CRUD operations.
Up until recently, I was under the impression that databases needed to be populated manually. For example, say I have an incredibly simple users table:
Now say I want to populate my database with 10 of these simple users. To do this, in my seeds.rb file I would need to type out the following 10 times, with different data:
On top of the incredible tediousness of typing this out for each user we want to create (and keep in mind, this is an incredibly simple table), who wants to sit and come up with all of those fake usernames and emails?!
It’s Faker to the rescue!!! Faker is a gem that lets you seed your database with, well, fake data.
As a new example, say we are building a real estate site. We have three tables; buyers, realtors, and apartments. Here’s what our schema looks like:
Note: I added some rows to the tables that don’t necessarily make sense, but just want to show some of the many different ways Faker can be used.
You can imagine what a nightmare it would be to populate a database like this manually… so let’s take a look at how the Faker gem can make our lives infinitely easier.
A few things to make sure you do before using the Faker gem:
- Make sure to run: gem install faker
2. Add this line to your Gemfile: gem ‘faker’, :git => ‘https://github.com/faker-ruby/faker.git', :branch => ‘master’
3. Run `bundle install` (if the gem wasn’t already in your Gemfile).
Now, let’s finally take a look at our seeds.rb file and see how easy Faker makes it to seed our database.
Note: At the top of ours seeds.rb file we are destroying all instances of our classes. This is helpful if you end up having to seed more than once but don’t want your database to become too overpopulated.
Now, after running `rails db:seed`, let’s take a look at our database tables:
Take a look at all of the awesome (and sometimes funny) data the Faker gem populates our database with! We could have even populated it however much more we wanted by changing the 10.times to any number we want! This blog really only scratches the surface of things you can seed with Faker. There are so many categories and subcategories of Faker data that you can seed with — you can see them all here.
One important thing to note- When seeding the above database I was getting some interesting results with some of the columns. I realized this was because things like phone number, credit card, and expiration which I had created as integers were being created by Faker as strings. This is an important thing to keep in mind when creating and seeding your tables.
To sum up, we often need our database to be populated in order to properly test our backend code. The Faker gem is an awesome tool to help us quickly and easily seed our database so that we don’t have to tediously do it by hand. I for one am grateful for the Faker gem, and I hope you now are too!