Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Manually setting a sequences numbers in the console. #615

davidvgus opened this Issue Feb 18, 2014 · 4 comments


None yet
2 participants

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.



joshuaclayton commented Mar 5, 2014

@davidvgus you can access sequences as such:

# given this sequence:
FactoryGirl.define do
  sequence :foobar

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 }

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.


joshuaclayton commented Mar 5, 2014

@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’m 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’t 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment