minifacture icon indicating copy to clipboard operation
minifacture copied to clipboard

Sequence error

Open ream88 opened this issue 14 years ago • 7 comments

Maybe I'm wrong, but shouldn't this code work?

Factory.define(:user) do |u|
  u.firstname 'John'
  u.lastname  'Doe%d'
  u.email     '%{firstname}.%{lastname}@example.com'
  u.password  'password'
end

100.times.map do
  puts Factory.build(:user).email
end

It returns [email protected] a 100 times. Weird…

ream88 avatar Nov 30 '11 20:11 ream88

Ok the problem isn't the counting method, the problem is that calling count/maximum before populating the database returns always 1, which then fails my specs. I'm working on a fix.

ream88 avatar Nov 30 '11 20:11 ream88

The issue is that you're building the records all at once and not saving them one at a time.

There are a lot of competing issues here. I use maximum :id instead of count because count decrements when records are destroyed, resulting in potential duplicates. The maximum :id will always be different so long as the records are saved.

You do bring up a good point, though a solution will need to spin off from a maximum :id implementation, not count.

stephencelis avatar Nov 30 '11 21:11 stephencelis

How about using a global count variable?

ream88 avatar Nov 30 '11 21:11 ream88

Sorry, should have commented on this earlier.

What if the number is stored on the factory itself, which increments once per "build"?

stephencelis avatar Jan 08 '12 20:01 stephencelis

Something like #8?

ream88 avatar Jan 09 '12 13:01 ream88

No, that delegates to Model.count.

stephencelis avatar Jan 09 '12 16:01 stephencelis

I reverted the repo, and github reverted the pull request too… so it's gone, but I will recreate it.

ream88 avatar Jan 09 '12 18:01 ream88