Save Time Seeding

Ben Hessel
3 min readNov 6, 2020

You don’t need to do everything the hard way.

While it’s good for programmers to understand what’s going on “behind the scenes” of our software, it can still be best practice to use gems that allow us to save time and shorten our code, by utilizing the programs that other coders have built.

Plus, frankly, this gem can be fun!

Setting up the Faker Gem

While I call this gem “fun,” it does solve some real problems.

What if you want seed data for your project and don’t want to write something like this dozens of times before connecting an API?

ben = Student.find_or_create_by(name: “Ben”, favorite_drink: “Coffee”, age: 31)

But you also don’t want fake data that looks fake and doesn’t represent the users who will ultimately be in your database.

With a couple of lines of code, you can use the Faker Gem to create as many, detailed seeds as you want.

Find full Faker Gem documentation here: (https://github.com/faker-ruby/faker)

Installation is only a few short steps. In your command line, first enter:

gem install faker

… and put gem ‘faker’ in your Gemfile. You may need require ‘faker’ at the top of your seed file as well.

Now to put this into practice, let’s say we want to generate 100 students for our seed file, and we want to get their first name, last name, age, and favorite dessert.

With Faker Gem, your code can be this simple:

…and there you have it! 100 random students and their favorite dessert.

And as you can see, the documentation is really easy to understand, in many instances offering sub-categories to select from as well:

What else can Faker do for you though, besides suggesting names and making you hungry for dessert?

I said FAKE credit card numbers…

You could use Faker::Finance.credit_card to generate fake credit card numbers.

Or Faker::DcComics.hero to get Batman and the rest of DC’s finest.

You can get locations — Faker::Games::SuperMario.location (told you this can be fun)

Quotes — Faker::Movies::Lebowski.quote

Songs — Faker::Music::RockBand.name

Characters — Faker::TvShows::SouthPark.character (or you could use this for names)

Even sounds — Faker::Creature::Dog.sound

So scroll back up, open the Faker documentation, and come up with a creative concept for your next personal project!

--

--

Ben Hessel

Aspiring Software Engineer, Student at the Flatiron School