I would like to be able to manually set the sequence number for a factory attribute when I'm in the console. I figure there is probably good reason why FactoryGirl doesn't let one manually set a sequence number but I'm not advanced enough to know what that reason is. I wrote a post about what I was trying to do and an easy work around but it seems like such a trivial change I thought I would ask about it.
@davidvgus you can access sequences as such:
# given this sequence:
FactoryGirl.define do
sequence :foobar
end
FactoryGirl.generate :foobar # => 1
FactoryGirl.generate :foobar # => 2
FactoryGirl.generate :foobar # => 3
This will increment by one and start with one. There's no way to start later on without changing the factory:
# given this sequence:
FactoryGirl.define do
sequence(:foobar) { |n| 1000 + n }
end
FactoryGirl.generate :foobar # => 1001
FactoryGirl.generate :foobar # => 1002
FactoryGirl.generate :foobar # => 1003
This seems a bit indirect compared to simply being able to initialize the sequence to a specific number.
@davidvgus indirect, yes - but doesn't require any change to the FG codebase. I'd be interested in hearing the use case for starting it at a higher number.
From a blog post I wrote about why I wished I had that functionality.
"For example I鈥檓 working on a project that uses FactoryGirl. I find that it is very helpful to be able to create complicated models in the console with a simple FactoryGirl.build or create command. But there is a problem. When using the console in dev mode the projects seeds have been created. In my case the factory for creating users generates unique email addresses using a sequence and adding a number to each. This is all well and good but when I create a message object in the console using the message factory it eventually has to create the associated user and this is where the problem arises. FactoryGirl doesn鈥檛 know that it has already created six users in order to seed the database so it attempts to start from the beginning and fails because of the uniqueness validation on the email address."
Basically FG is great for creating objects in the rails dev console. But the problem is it will start sequences from the beginning rather than picking up where the FG fixtures left off. Maybe this doesn't bother other people because they don't use FG on the dev console?
Has this been resolved in some way?
I have also run into this scenario, and think it would be a great addition if it doesn't exist somewhere now. I have found myself taking advantage of console for rapid testing analysis. Even simply offering an extra argument to the create_list to offset the sequence would be more than sufficient.
Most helpful comment
From a blog post I wrote about why I wished I had that functionality.
"For example I鈥檓 working on a project that uses FactoryGirl. I find that it is very helpful to be able to create complicated models in the console with a simple FactoryGirl.build or create command. But there is a problem. When using the console in dev mode the projects seeds have been created. In my case the factory for creating users generates unique email addresses using a sequence and adding a number to each. This is all well and good but when I create a message object in the console using the message factory it eventually has to create the associated user and this is where the problem arises. FactoryGirl doesn鈥檛 know that it has already created six users in order to seed the database so it attempts to start from the beginning and fails because of the uniqueness validation on the email address."
Basically FG is great for creating objects in the rails dev console. But the problem is it will start sequences from the beginning rather than picking up where the FG fixtures left off. Maybe this doesn't bother other people because they don't use FG on the dev console?