Faker Gem producing blank names randomly

739 views Asked by At

Versions: faker 1.4.3 ruby ruby 2.1.3p242 rails 4.0.0

I've been using Faker for a while now without issues. However, I am at the point where I would like to begin validating for string presence/lengths and I am running into an issue with Faker. I currently have a rake task written to generate a bunch of a sample data. Here's a function where I am creating users:

def make_users(agency)
  10.times do |n|
    first_name = Faker::Name.first_name
    last_name = Faker::Name.last_name
    email = rand(100).to_s + "" + Faker::Internet.email
    password = "password1"
    agency.users.create!( 
      first_name: first_name,
      last_name: last_name,
      email: email,
      password: password,
      password_confirmation: password)
  end
end

In the model for User, I now require that the user enters a first and last name (as well as other validations for password and email).

validates :first_name,  presence: true, length: { maximum: 50 }
validates :last_name,  presence: true, length: { maximum: 50 }

However, when I run my rake task to generate users, about 10% of the time Faker will try to create a user with an empty first or last name. Because my script creates 10 users, it hardly ever runs without giving me:

ActiveRecord::RecordInvalid: Validation failed: First name can't be blank

or the equivalent error for Last name.

I've Googled around and haven't seen anyone else complain of this.

0

There are 0 answers