DEV Community

Cover image for Seeding your rails database using Faker
Daniel Zaltsman
Daniel Zaltsman

Posted on

Seeding your rails database using Faker

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

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 logo faker-ruby / faker

A library for generating fake data such as names, addresses, and phone numbers.

logotype a happy-07

Faker

Tests Gem Version Inline docs Test Coverage Maintainability SemVer compatibility

This gem is a port of Perl's Data::Faker library that generates fake data.

It comes in very handy for taking screenshots (taking screenshots for my project, Catch the Best was the original impetus for the creation of this gem), having real-looking test data, and having your database populated with more than one or two records while you're doing development.

NOTE

  • While Faker generates data at random, returned values are not guaranteed to be unique by default You must explicitly specify when you require unique values, see details Values also can be deterministic if you use the deterministic feature, see details
  • This is the master branch of Faker and may contain changes that are not yet released Please refer the README…

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

Top comments (3)

Collapse
 
scrabill profile image
Shannon Crabill

I love Faker. Thanks for sharing. I'll be using it in upcoming Rails projects too.

Collapse
 
danimal92 profile image
Daniel Zaltsman

I'm glad I was able to help! Woohoo!

Collapse
 
danimal92 profile image
Daniel Zaltsman • Edited

The only datatype attributes that a rails model accepts is:
:binary
:boolean
:date
:datetime
:decimal
:float
:integer
:bigint
:primary_key
:references
:string
:text
:time
:timestamp

If you needed to attach a profile image to each user, I would recommend hosting/using an image online and attaching the url as a string to the user as an attribute. But, as far as I know, you can't attach the actual file to the user model. But when you're loading the profile picture, you can use the link to load it. You'll have to change your createusers migration file to store the url, drop the table, create the table, and migrate again. So to change the file, it might look something like this:

class CreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      t.string :name
      t.string :profile_url
      t.timestamps
    end
  end
end

Afterwards, delete your schema if you have one, and run rails db:drop && rails db:create && rails db:migrate in the console. Now you can store the url in that string and use it to populate the profile picture area.