Seeding your rails database using Faker

danimal92

Daniel Zaltsman

Posted on October 21, 2019

Seeding your rails database using Faker

Recently, I attended a hackathon to test my programming grit. The focus of it was to build and/or create an innovative app regarding civic issues in a span of 6 hours.

We decided to use Ruby on Rails to create our website. After building out our skeleton, we needed to have some sample data for testing. Writing out the tests manually in the seed file and instantiating our model objects turned into quite a slog. We ended up with too small of a sample size, and it limited our ability to discover edgecases, errors, and the means to show off the project.

Enough was enough. I decided it was time to learn from my mistakes, automate this process, and wash my hands of this time sink. So if you're using rails and you haven't found a good way to seed your data with it yet, this is for you.

My first instinct was to turn to a gem I had seen before called Faker. It randomly generates fake, but relevant, data to seed your files as long as you choose the right libraries. Let's get it started.

Step 1: Installing the gem

If you have Ruby installed, go ahead and type the following in your terminal to install Faker:

gem install faker

Step 2: Requiring the gem

Great! Now in order to use it, go into your project's gem file and require it, like so:

require 'faker'

Now run a bundle install in your terminal while it's accessing your project directory:

bundle install

Step 3: Planting the seeds

And last but not least, go to your seed file and use the create method along with the times method to create multiple objects:


10.times do

Character.create(name: Faker::Movies::LordOfTheRings.character)

end

I also added a location to my characters. Here's the result:

Alt Text

Beautiful.

You can also chain .unique right before the method call to ensure there is no repetition of data:


10.times do

Character.create(name: Faker::Movies::LordOfTheRings.unique.character)

end

I definitely encourage you to use this, and to look through the gem's github and libraries to see its full capability.

GitHub logo faker-ruby / faker

A library for generating fake data such as names, addresses, and phone numbers.

logotype a happy-07

Faker

Tests Gem Version Inline docs Test Coverage Maintainability SemVer compatibility

This gem is a port of Perl's Data::Faker library that generates fake data.

It comes in very handy for taking screenshots (taking screenshots for my project, Catch the Best was the original impetus for the creation of this gem), having real-looking test data, and having your database populated with more than one or two records while you're doing development.

NOTE

  • While Faker generates data at random, returned values are not guaranteed to be unique by default You must explicitly specify when you require unique values, see details Values also can be deterministic if you use the deterministic feature, see details
  • This is the master branch of Faker and may contain changes that are not yet released Please refer the README…

It would be an absolute waste of time for you not to learn from my mistakes, and continuing to sit there, writing out line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line...

Alt Text

💖 💪 🙅 🚩
danimal92
Daniel Zaltsman

Posted on October 21, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related