Sequence error
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…
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.
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.
How about using a global count variable?
Sorry, should have commented on this earlier.
What if the number is stored on the factory itself, which increments once per "build"?
Something like #8?
No, that delegates to Model.count.
I reverted the repo, and github reverted the pull request too… so it's gone, but I will recreate it.