Making use of the Faker gem

Johnny Cao
3 min readFeb 8, 2021
Professional League of Legends player “Faker”. He is widely considered the best LoL player

The Faker gem is a useful ruby gem that produces data at random, this can be useful when you’re in need of data, but don’t want to use an API. Imagine you need to fill out users with E-mails or if they need different data for seeding purposes? This ends up being tedious to hard code and implementing an API can be difficult.

This is where the gem comes in, Faker is an open-source gem for generating fake data. Say you’re trying to work on an API involving Japanese media (or Anime, but you get the idea). Faker would have you covered, being able to add real data to your library.

An example of the options given in the Faker gem file

Now that you’ve seen how the Faker gem works, you might be wondering; how do I get started? For starters, you can find the gemfile here. Make sure your gemfile includes gem “faker”

Here's what my gemfile looked like

After that, you can begin installing Faker by running the following:

gem install faker

Now that the Faker gem is installed, you can play around with it in your seeds.rb file. However, take note that the data is generated at random, meaning that you will not be given the same data every time you run your database. Hence why Faker giving out “fake” data is the purpose of this ruby gem.

Here’s an example of what a seed file could look like with the Faker gem

After implementing your own seeds, make sure to run rake db:seed in your terminal. After that, your randomly generated data should be good to go! Be sure to look through the different generators Faker has to offer a few times. It took me a few reads before I noticed that even League of Legends was an option for data (this being the person I referenced the gem name to earlier). Below will be a few more examples of topics you can cover.

Examples of games from the Faker gem
Examples within the League of Legends topic in the Faker gem

By now, I hope you’ve seen the power of the Faker gem and how you can make use of it. It was a lifesaver on one of my projects, as I needed to seed random user data and review data; Faker proved to be perfect for this occasion. Happy coding :)

Sources: https://github.com/faker-ruby

--

--