I am using FactoryGirl and Faker to generate user objects in my seeds.rb
file but for some reason the exact same user is being created and rake db:seed
is failing because of an email uniqueness validation.
Factory for users:
#users.rb
require 'faker'
FactoryGirl.define do
factory :user do
first_name Faker::Name.first_name
last_name Faker::Name.last_name
phone Faker::PhoneNumber.cell_phone
email Faker::Internet.email
password "password"
password_confirmation "password"
end
end
And the code in seeds.rb
file:
#seeds.rb
rand(5..11).times { FactoryGirl.create(:user) }
Error:
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
If I open the console and use FactoryGirl.create(:user)
I get the same results...same object is being created over and over even though if I run just Faker::Internet.email
several times I'll get several e-mails.
FactoryGirl:
[1] pry(main)> FactoryGirl.create(:user)
...
=> #<User id: 3, first_name: "Osvaldo", last_name: "Wunsch", email: "[email protected]", phone: "(912)530-4949 x64848", created_at: "2014-07-31 20:57:27", updated_at: "2014-07-31 20:57:27", encrypted_password: "$2a$10$mxWC7yLYR0m/Sw8MO6Lyru.xuTHCdCEuM9Orx3LXGApF...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil>
[2] pry(main)> FactoryGirl.create(:user)
...
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
Faker by itself:
[3] pry(main)> Faker::Internet.email
=> "[email protected]"
[4] pry(main)> Faker::Internet.email
=> "[email protected]"
What am I missing here? Why is Faker producing the same data every time when used through FactoryGirl?