Maria Coitinho
6 min readNov 19, 2022

Seeding data: Ruby’s Faker gem

When we intend to build a database, first we usually create a table and give it columns with specific names that represent that data. For example, if we want to create a database for all the clothes in a store, we could represent it with a name, price and size columns. The rows of this table would represent a particular clothing item — we say particular because the id is unique to the item:

Above we have a representation of a database table through VisualStudio SQLite extension, used to better visualize our table data. It is easier when we already have real data of clothes from a clothing store. Now imagine that we are working on a database project or even testing some database manipulation commands.

Creating a table and coming up with examples when we are testing out code can take some of our precious time to think what dummy data we can add to our rows.

We don’t want to spend time with that, as developers we have much more important things to worry about, like our own data manipulation and how we can build a well structured database.

Ruby Object Oriented programming language along with Active Record built in methods allows us to interact with a database and therefore, use commands from the terminal to create tables and data rows without needing to explicitly write SQL.

Moving forward I will go through an example on how to populate sample data to a table — by convention the file used to write Ruby commands for creating data rows is called seeds.rb.

Note: Keep in mind that there’s more configuration needed prior to have the steps below working smoothly, for example the installation of gems like ActiveRecord and Faker in our Ruby environment. We also need to go through other important steps like establishing a connection with the database through some code, having a data manager like SQLite, having all required files, migration knowledge, gems(like Rake) and name conventions set up.

Populating a Database

Let’s suppose that we have a database table of a coffee shop that has the following columns to describe a coffee: blend name, origin and variety. Our table is going to be called stores_coffees — by convention the name of a table should be the plural of a class representing that table. In our case, this class is located in a file called stores_coffee.rb, containing the StoresCoffee class which inherits from ActiveRecord::Base:

Going into the file that holds the code for creating the table through a rake migration, we have:

Alright, now we have our stores_coffees table but we cannot do a lot with a coffee store that doesn’t have coffees, so let’s add some coffee data to it! For that, we will use the convention of storing data in a seeds.rb file and pass the attributes of the coffee as arguments to the create method:

Now, we can use the following Rake command to populate this data into our database table:

$ bundle exec rake db:seed

If you open the database file with the SQLite extension for VisualStudio to visualize our data, we have:

Great! We populated our own data to our coffee shop. However, I had to type all of it, which is not too practical if we need to have a table with 20 different types of coffees.

Faker gem

That’s when the Ruby Faker gem comes in handy. Faker is a library that generates random data, so if we are looking to have 20 different coffee types we can seed our table easily writing just a few lines of code.

To install Faker run this command in your terminal:

$ gem install faker

Note: If you are using a Gemfile for your gems, don’t forget to specify the Faker gem in there.

To generate sample data Faker disposes of different topics. You can check a full list trough the link: https://github.com/faker-ruby/faker#generators.

If we take a look at the list of generators, we can find Faker::Coffee. Clicking on it, redirects us to a list of methods that can be called on the Coffee class to generate data based on the name of the method:

Let’s try it out! I will be using Pry as a REPL so you can check the return values from calling the Faker::Coffee class with its built-in methods:

Above we called the .blend_name, .origin and .variety methods on the Coffee class from the Faker module. If you noticed, these methods are exactly the attribute names we created for our table columns. Awesome, we can use them to generate as many coffee data as we want!

Going back to our seeds.rb file, we can now create 20 coffee sample data wihthe following Ruby code:

We used the .times loop along with the .create method to generate 20 StoresCoffee instances and save its data to our database table.

Note: If you already seeded the table with the 3 coffee datas that we created previously in this post, you can do:

$ bundle exec rake db:rollback

Then, run again the following code but now with the new Ruby loop we just created(located in seeds.rb):

$ bundle exec rake db:seed

Now that we are finally able to populate our table with 20 coffees, we can take a look at it with SQLite in Visual Studio:

Using Ruby’s Faker gem allows us to generate random data without us having to spend time thinking of examples. We just wrote a few lines of code and looped through it, each time generating a new random data.

You can test it out retrieving information from the table using some ActiveRecord built-in methods in a REPL:

Conclusion

In this post we went through:

  • Generating and saving data into a database table with a few data examples;
  • The Faker GEM and its ability to generate many data rows using Ruby loops along with ActiveRecord’s built in methods.

Although the data retrieved from Faker is not the most realistic one — we can achieve getting realistic data with APIs — it is still a very useful library when we need to create data examples related to some sort of topic.