Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Faker gem to generate correlated city, postal code, country code values

Is there a way to get the Faker gem to generate 'correlated' city and country code values?

For example,

  • Vancouver, CA
  • Minneapolis, MN

I'm doing this:

FactoryGirl.define do
  factory :location do
    ...
    city {Faker::Address.city}
    country_code {['US', 'CA'].sample}
    ...
  end
end

But there is no guarantee that the city will actual reside in country_code.

I'd settle for something like:

postal_code {Faker::Address.postcode(['US', 'CA'].sample) }

Which I could then geocode to get the other values.

like image 750
craig Avatar asked Jul 25 '14 22:07

craig


People also ask

What Is Faker gem?

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

How do you use faker in Ruby on Rails?

Installation of the faker gem is very straightforward. The Faker README is very detailed and provides a great deal of information. To install the faker gem simply type gem install faker in your terminal. After the gem installation is complete, open your rails app and go to your seeds file to begin using Faker.


1 Answers

You can customize based on locales: https://github.com/stympy/faker#customization

So you can create or modify a locale file, see this example: https://github.com/stympy/faker/blob/master/lib/locales/en-NEP.yml

Then you can set what the default country should be and also what states do you want to use

like image 138
Aguardientico Avatar answered Sep 19 '22 10:09

Aguardientico