Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate coherent data with Rails Faker gem?

In order to populate my Rails application with fake data I often do this:

person = Person.create(
  :first_name   => Faker::Name.first_name,
  :last_name    => Faker::Name.last_name,
  :email        => Faker::Internet.email
)

This might produce a person like:

First name: Olivia

Last name: Kubera

Email: [email protected]

Is there a way to generate more coherent fake data like:

First name: Olivia

Last name: Kubera

Email: [email protected]

Or will I have to come up with something of my own to achieve this?

like image 228
Tintin81 Avatar asked Feb 13 '14 19:02

Tintin81


1 Answers

You can pass a name to Faker::Internet.email to generate a fake email address for that person. You'll have to do a bit more work but not too much:

first_name = Faker::Name.first_name
last_name = Faker::Name.last_name
person = Person.create(
  :first_name => first_name
  :last_name => last_name
  :email => Faker::Internet.email(first_name + "." + last_name)
)
like image 170
Shadwell Avatar answered Oct 05 '22 20:10

Shadwell