Happy Seeding with Faker
Tags: ruby, tutorial, database, beginners
Published: 2022-12-13 17:33:42

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'](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k52d5bdao9wpuborzoxr.png)


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

![Gem install faker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqrascocazeqjvx96lk8.png)


**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'](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/haaenbg06j3a2511d3vx.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpij45k2nayfv88l5zmi.png)




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!!! 🌱🌱🌱🌱



**_References_**

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

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





![Faker gem](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/470jgfqjsawvxv3y4te2.png)