DEV Community

Cover image for Happy Seeding with Faker
Marcos Freitas
Marcos Freitas

Posted on

Happy Seeding with Faker

Learning Ruby as a backend language in the first couple of days, seemed "easy" enough to be true. Obviously its syntaxes and functionalities eventually caught up to me. It's amazing to learn the importance, understanding, and planning relationships between backend and frontend datas in order for you to trace your project's paths. Just like in the front end, the backend has a bunch of tools to help make your code more dynamic, cleaner, and more organized. Tools that can save you so much time writing backend data and focus more on your project creativity in the frontend.

Having that in mind, the process of creating your backend data in the seeds.rb file, can be just as simple and easy as creating some random dummy data for a small project, or it can be harder and more time consuming trying to create extensive dummy data for a bigger project.

Therefore, I will introduce you to "Faker". A Ruby gem that only works with ruby applications and generates fake information for a bunch of different things such as; names, addresses, games, movies, books, etc. This gem can and will save you a lot of time manually putting placeholder data into your seeds.rb file.

Here are the steps to have your Ruby gem Faker installed:

STEP 1
Make sure you INSTALL (gem 'faker') in your Gemfile and run (bundle install) in your project's directory.

Installing gem 'faker'

Or, another option would be to run (gem install faker) on your machine terminal.

Gem install faker

STEP 2
Go to your seeds.rb file inside your db directory and add (require 'faker') on the top of your file.

Adding require 'faker'

Presuming your tables are created and migrated, you can generate some "Faker" data with correct format to use in your projects.

In your seeds.rb file, you have to set a time loop to the number of times you want to generate data. In this case, it is “5” for Earthling. The faker gem will create new random data for every field.

Faker gem data syntax

After you created a seeds.rb file containing all your data, do not forget to seed it!

In your terminal (project's directory) run:

rake db:seed

You can check everything was seeded correctly by confirming your data is present within the rake console and run tests.
For that all you need to do is in your project's directory run, rake console , then run tests.

Note:
If no seed data shows up, see that you are meeting all validations in your models that may be prohibiting the data from being created in the first place.

In case you need to create data that there are not specific in Faker generators, get creative with the ones that already exist. Most generators provide multiple methods for various types of, as well as unique data.

Conclusion

Time is of essence and creating seed data can be a long and boring task, but it doesn't have to be anymore! Using this ruby Faker gem, writing seed data will be simpler, and faster.

                 Happy Seeding!!! 🌱🌱🌱🌱
Enter fullscreen mode Exit fullscreen mode

References

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

https://github.com/brewchetta/phase-3-practice-code-challenge-aliens-among-us

Faker gem

Top comments (1)

Collapse
 
fwolfst profile image
Felix Wolfsteller

Just one addition:

A Ruby Gem that only works with Ruby applications.

Faker implementations exist also in perl (metacpan.org/dist/Data-Faker - i think that was the original implementation), JS (fakerjs.dev/), Java, Python and probably some other languages.