“Sincerity — if you can fake that, you’ve got it made.”
― George Burns
If there is one thing we can all agree upon, it is that George Burns was clearly a sociopath. Let’s just address that elephant in the room before we kick off.
Luckily, that quote of his has no direct relevance to the subject matter in this post.
However, that quote was generated from a large collection of quotes under the #fake, #faker, and #fake-quotes categories on Google, and then chosen at random to fill the role of “quote at the top of the page”.
And that is the kind of functionality we will be addressing today.
Let’s talk about FAKERS, shall we?!
The RUBY Faker gem is a dynamic tool that can be used when creating any app that plans to harvest and utilize information from a database. So basically, any app.
Essentially, a Fakers purpose is to generate data that is, you guessed it, fake. This data will assume the role of valid data that has not yet been either collected or pulled from an outside source such as an external API. It lives happily in your seeds.rb file, churning out Users by the name of “Flurg Bumblstorm” and “Jeno Kobinson” for the purpose of testing your code, hopefully resulting in more satisfying, interesting and of course useful drafts as you build. And for the record, names like “Polo Yohanusgoff” are the names that DON’T fall under the specific “Funny Names” category.
To use Faker in the build of your next app, you want to add it into your Gemfile. If not already installed, you’ll want to open up your terminal and run:
gem install faker
Or, you can head over to your actual Gemfile, and somewhere among the ranks of your other precious gems, type in:
Then head back to the terminal and run your <bundle install>. Once either of these steps are complete, consider yourself ready to create more fake data than Enron ever did.
Now let’s dive in to actually using the Faker gem.
First, you want to consider the attributes with which your models are created. For the purposes of the app I am currently building in my Phase 2 project at the Flatiron School, my partner and I needed to generate an instance of an Actor. An actor needed to be created with a name, bio, an actor’s website and an image of the actor.
Amazingly, Faker has the ability to generate random instances of every. single. one.
Written out, the Faker code looks like this:
As those of you familiar with code may notice, what is happening above is quite standard — for the rest of you, why did you even come to this article? We are assigning the values given to us by the Faker gem to variables, then plugging those variables into a .create method for the class of Actor. When seeded in our terminal, this will generate 5 new instances of Actors, each with a unique set of attributes that we can then use to help form our more complex code and develop app functionality.
But these are not the only categories the Faker gem can generate — oh no my friend. The list is quite extensive, ranging from books to Pokemon to Family Guy references (you can actually pull out characters and locations from many shows!) and it can all be found here.
Simply plug in the various Faker generators as you see fit within your seeds.rb file, apply them to your own version of an Actor instance, and voila! You have successfully created seed data without the need of a pesky API (APIs are awesome and not pesky) or a monotonous coding session writing out fake and often uninspired data.
The Faker gem is a huge time saver and an excellent tool for seeding in data. Ultimately, that data will never see the light of day, and it will make your building experience more dynamic as it simulates what your database and app will ultimately look like.