When I first began using Ruby on Rails I realized that compared to Rack, and Sinatra there was some serious streamlining and automation. I found that Rails could cut the amount of time spent writing boilerplate code into almost zero.
With this in mind, one of the attributes that I like about being a programmer is that it's good to be lazy.
With Rails, I could be lazy and still get a lot of groundwork covered. This gave me more time to spend on solving more complicated problems related to my app.
But… even with all this streamlining there are still some areas of building out the back-end that take up more time than it should.
Seeding a Database
One area that is particularly tedious is creating seed data that will be useful and can actually represent what might actually exist in a fully deployed project.
On the one hand I want to save time and on the other I want enough data that is reflective of the general randomness that users can bring to an app.
Luckily the awesome members of the ruby community can always be relied on to swoop in and save the day… this time it’s with the Faker gem.
As ridiculous as this may sound one aspect of creating seed data that is so tedious is thinking up random names and/or other information related to my models.
With the Faker gem this is all taken care of for me.
Faker is a gem that you can use to create “fake” data. You can then use that data when you write your seed file.
okay the bottom one is clearly easier to just copy and paste …but what does it return?
Here's an example of just three new users:
Name: Elfreda Volkman, Job: Central Manager, quote: Imagine you are writing an email. You are in front of the computer. You are operating the computer, clicking a mouse and typing on a keyboard, but the message will be sent to a human over the internet. So you are working before the computer, but with a human behind the computer.Name: Ethan Gulgowski, Job: IT Liaison, quote: People are different. People choose different criteria. But if there is a better way among many alternatives, I want to encourage that way by making it comfortable. So that's what I've tried to do.Name: Beverley Harris III, Job: Regional Officer, quote: Everyone has an individual background. Someone may come from Python, someone else may come from Perl, and they may be surprised by different aspects of the language. Then they come up to me and say, 'I was surprised by this feature of the language, so therefore Ruby violates the principle of least surprise.' Wait. Wait. The principle of least surprise is not for you only.
Thats a lot of data… its a lot easier letting the gem do it for me.
But as I said earlier I’m lazy, so if all I had to do was copy and paste it as many times as I needed data, than I could just loop it!
That's a lot of seed data all in three lines!
The anatomy of a Faker
So what does it take to be a Faker? …well it’s actually quite simple:
First, you have to install the gem itself
gem install faker
Second, you just require it in your seed.rb file
Third, when you create a new instance to be saved in your database, rather than passing in actually hand-typed data, you use the Faker syntax like so:
Dog.create(name: Faker::Creature::Dog.name, breed: Faker::Creature::Dog.name, age: Faker::Creature::Dog.age)
So as you can see you use the Faker module, within the namespace “Creature” and then within the namespace of “Dog”, and finally append it with the desired trait, like: name, age, breed, etc.
Now obviously there is a limit to what can be done with Faker. You can’t have a random made up trait like:
Faker::Creature::Dog.favorite_video_game==> `handle_exception': translation missing
This may make the gem seem less useful, but in truth there are a lot of different namespaces to use.
Looking at the docs there are 165 different namespaces all with their own attributes.
From things like seinfeld characters (with traits like name, quote, company) to lists of fake phone numbers.
So hopefully the gem will have something you need.
If it doesn’t have what you need you can always contribute…