Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3633480
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:39:30+00:00 2026-05-19T00:39:30+00:00

Provided that I have a project factory Factory.define :project do |p| p.sequence(:title) { |n|

  • 0

Provided that I have a project factory

Factory.define :project do |p|
  p.sequence(:title)    { |n| "project #{n} title"                  }
  p.sequence(:subtitle) { |n| "project #{n} subtitle"               }
  p.sequence(:image)    { |n| "../images/content/projects/#{n}.jpg" }
  p.sequence(:date)     { |n| n.weeks.ago.to_date                   }
end

And that I’m creating instances of project

Factory.build :project
Factory.build :project

By this time, the next time I execute Factory.build(:project) I’ll receive an instance of Project with a title set to "project 3 title" and so on. Not surprising.

Now say that I wish to reset my counter within this scope. Something like:

Factory.build :project #=> Project 3
Factory.reset :project #=> project factory counter gets reseted
Factory.build :project #=> A new instance of project 1

What would be the best way to achieve this?

I’m currently using the following versions:

factory_girl (1.3.1)
factory_girl_rails (1.0)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:39:31+00:00Added an answer on May 19, 2026 at 12:39 am

    After tracing my way through the source code, I have finally come up with a solution for this. If you’re using factory_girl 1.3.2 (which was the latest release at the time I am writing this), you can add the following code to the top of your factories.rb file:

    class Factory  
      def self.reset_sequences
        Factory.factories.each do |name, factory|
          factory.sequences.each do |name, sequence|
            sequence.reset
          end
        end
      end
      
      def sequences
        @sequences
      end
      
      def sequence(name, &block)
        s = Sequence.new(&block)
        
        @sequences ||= {}
        @sequences[name] = s
        
        add_attribute(name) { s.next }
      end
      
      def reset_sequence(name)
        @sequences[name].reset
      end
      
      class Sequence
        def reset
          @value = 0
        end
      end
    end
    

    Then, in Cucumber’s env.rb, simply add:

    After do
      Factory.reset_sequences
    end
    

    I’d assume if you run into the same problem in your rspec tests, you could use rspecs after :each method.

    At the moment, this approach only takes into consideration sequences defined within a factory, such as:

    Factory.define :specialty do |f|
      f.sequence(:title) { |n| "Test Specialty #{n}"}
      f.sequence(:permalink) { |n| "permalink#{n}" }
    end
    

    I have not yet written the code to handle: Factory.sequence…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a VXML project that a 3rd party parses to provide us with
Given a situation where you have 2 projects that in total will provide enough
If I have a subclass that has yet to implement a function provided by
HI All, I have a piece of javaScript that removes commas from a provided
I have a project that uses MVC 3 and Entity Franework. The site works
Do the major web application frameworks (Rails, Django, etc) have libraries that provide functionality
I have a C++ library that provides various classes for managing data. I have
I have a kernel module that provides data to a userland process through read().
I want to have a select-only ComboBox that provides a list of items for
I have a complex .NET Remoting server app that provides a couple of services.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.