newBike
newBike

Reputation: 15002

How to generate different attributes everytime

In my code , the 100 stores are sharing the same attributes.

I thought it should create a different one every time.

(1..100).each do
  store_attr = FG.attributes_for :store
  store_attr[:account] = accounts.sample
  stores << Store.create(store_attr)
end

FactoryGirl.define do
  factory :store do
    name Faker::Company.name
    latitude 1.5
    longitude 1.5
    street Faker::Address.street_address
    city Faker::Address.city
    state Faker::Address.state
    zip_code Faker::Address.zip_code
    phone Faker::PhoneNumber.cell_phone
    email Faker::Internet.email
    website "https://#{Faker::Internet.domain_name}"
    account nil
    factory :complete_store do
      name 'store_with_account'
      account
    end
  end
end 

Upvotes: 0

Views: 80

Answers (1)

oowowaee
oowowaee

Reputation: 1695

I believe you need to put them inside a block -

street { Faker::Address.street_address }

Otherwise they will only be generated once and reused for all instances. You can read more here.

Upvotes: 1

Related Questions