Phase 3 - Seeding Ruby Application Using Faker

Seeding fake data into your Ruby application can be an excellent way to start testing out your migrations, methods, and controllers without having to take all the time to write up a bunch of fake data, or build out your forms in order to submit data from the front end. This blog will walk through the basic uses for Faker and then also explore some more advanced uses if more complex or more specific kinds of data are needed.

Installing the GEM

The first step is to install the Ruby gem for ‘faker’. You can do this by navigating to the folder where your code is stored in your terminal, and then typing # gem install faker (without the pound sign) into your terminal. This will add the gem ‘faker’ to your Gemfile folder (feel free to check if you’d like).

From here it’s as easy as adding # require ‘faker’ to the top of the files you are going to call it from.

What now? How do I use it?

This is where things get fun! Faker is a very interesting and sometimes downright entertaining gem to use. What fakers are you in the mood to use? Are you building an application to list al Dr Who episodes? You’re in luck! The faker gem has countless options to “fake” information down to very specific topics.

If you’re curious and want to see what fakers are available, take a peak at the Git Hub Repository. They range from your favorite movies, video games, and animals to broader topics like “famous last words”. You can find just about whatever you might need to seed your database filled with interesting fake information.

To clarify, that’s what faker is for. Quite literally, it will fill your database with as much fake information as you need. So, while the information is fake and in some instances, it doesn’t matter as much what you’re seeding your database with so long as the values fill the table and you know what they represent.

Now, let’s take about how to actually use the gem. Here is the code that I wrote to seed my database before I got to work seeding it via the front end forms:




As you can see, in this instance I wasn’t being too picky about the kind of data I seeded with (lorem sentences and using general fake names for rides). I was also able to add the 10.times method to control the number of data points that I wanted to be added and without having to repeat the code on many lines. Awesome!

Using Faker for “has many” (nested) seeding

One thing that I didn’t do that I definitely should have was to seed my database with the “has many” relationship information all at once. Here is how I could have done that using a book and review example:

First, instead of calling the faker gem inside the ‘times’ loop, I could have created methods that could be called within the loops - allowing for less repetitive code. An example might look like this:




Conclusion

There you are - how cool is Faker? There are many more interesting and entertaining features that Faker helps do for us, so I encourage you to go take a look yourself and read up on this awesome Ruby gem tool.

Next
Next

Phase 1 - Fetch