Seeding your rails database using Faker
Tags: seeding, gem, ruby, rails
Published: 2019-10-21 20:33:39

Recently, I attended a hackathon to test my programming grit. The focus of it was to build and/or create an innovative app regarding civic issues in a span of 6 hours. 

We decided to use Ruby on Rails to create our website. After building out our skeleton, we needed to have some sample data for testing. Writing out the tests manually in the seed file and instantiating our model objects turned into quite a slog. We ended up with too small of a sample size, and it limited our ability to discover edgecases, errors, and the means to show off the project. 

Enough was enough. I decided it was time to learn from my mistakes, automate this process, and wash my hands of this time sink. So if you're using rails and you haven't found a good way to seed your data with it yet, this is for you. 

My first instinct was to turn to a gem I had seen before called Faker. It randomly generates fake, but relevant, data to seed your files as long as you choose the right libraries. Let's get it started. 

##Step 1: Installing the gem

If you have Ruby installed, go ahead and type the following in your terminal to install Faker: 

>gem install faker

##Step 2: Requiring the gem

Great! Now in order to use it, go into your project's gem file and require it, like so:

>require 'faker'

Now run a bundle install in your terminal while it's accessing your project directory:

>bundle install

##Step 3: Planting the seeds

And last but not least, go to your seed file and use the __create__ method along with the __times__ method to create multiple objects:

```

10.times do

Character.create(name: Faker::Movies::LordOfTheRings.character)
 
end

```

I also added a location to my characters. Here's the result:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/p8iw0j28kpbxmsepspki.png)

Beautiful.

You can also chain **.unique** right before the method call to ensure there is no repetition of data:

```

10.times do

Character.create(name: Faker::Movies::LordOfTheRings.unique.character)
 
end

```

I definitely encourage you to use this, and to look through the gem's github and libraries to see its full capability.{% github faker-ruby/faker %} It would be an absolute waste of time for you not to learn from my mistakes, and continuing to sit there, writing out line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line, after line...


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/kxd1z980m9ddpkwvkq4r.gif)