Andrew Lindner     Blog     Archive     Reading     About

Quickly Seed Your Database With Faker

For those unacquainted with it, Faker is a Ruby gem that developers can use to generate random data from a multitude of categories. For example, users can direct Faker to retrieve a random quote from an ancient Greek philosopher or the name of a planet from the Star Wars universe. In an effort to efficiently test and inject some creativity in my applications, I often incorporate Faker in my backend seed file.


INSTALLATION

To set up the Faker gem, simply run the following command:

gem install faker

Alternatively, place the following line in your project’s Gemfile (run bundle install after updating the Gemfile):

gem 'faker'

Lastly, insert this line at the top of the seeds.rb file:

require 'faker'

EXAMPLES

Here are a few example use cases to demonstrate Faker’s versatility and usefulness in efficaciously prototyping an application.

Usernames:

Faker::Movies::StarWars.character
=> "Anakin Skywalker"

Faker::Books::Dune.character
=> "Paul Atreides"

Faker::Movies::LordOfTheRings.character
=> "Saruman the White"

Quotations:

Faker::GreekPhilosophers.quote
=> "There was never a genius without a tincture of madness."

Faker::TvShows::Seinfeld.quote
=> "She’s one of those low-talkers. You can’t hear a word she’s saying!"

Faker::TvShows::BojackHorseman.tongue_twister
=> "Courtly roles like the formerly portly consort are Courtney Portnoy's forté"

Locations:

Faker::Movies::HarryPotter.location
=> "Number 12, Grimmauld Place"

Faker::Address.state
=> "Maine"

Faker::Games::Zelda.location
=> "Lanayru Heights"

SOURCES

Faker Documentation