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 5927943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:14:12+00:00 2026-05-22T14:14:12+00:00

I have 2 factories. Beta_user and Beta_invite. Basically before a Beta_user can validly save

  • 0

I have 2 factories. Beta_user and Beta_invite. Basically before a Beta_user can validly save I have to create an entry of Beta_invite. Unfortunately these models don’t have clean associations, but they do share an email field.

Factory.sequence :email do |n|
  "email#{n}@factory.com"
end

#BetaInvite
Factory.define :beta_invite do |f|
  f.email {Factory.next(:email)}
  f.approved false
  f.source "web"
end

#User
Factory.define :user do |f|
  f.email {Factory.next(:email)}
  f.password "password"
end


#User => BetaUser
Factory.define :beta_user, :parent => :user do |f|
  f.after_build do |user|
    if BetaInvite.find_by_email(user.email).nil?
      Factory(:beta_invite, :email => user.email)
    end
  end
end

So in the beta beta_user factory I am trying to use the after_build call back to create the beta_invite factory.

However it seems to be acting async or something. Possibly doing the find_by_email fetch?

If I try this:

Factory(:beta_user)
Factory(:beta_user)
Factory(:beta_user)

I get a failure stating that there is no record of a beta_invite with that users email.

If instead I try:

Factory.build(:beta_user).save
Factory.build(:beta_user).save
Factory.build(:beta_user).save

I get better results. As if calling the .build method and waiting to save allows time for the beta_invite factory to be created. Instead of calling Factory.create directly. The docs say that in the case of calling Factory.create both the after_build and after_create callbacks get called.

Any help is much appreciated.

UPDATE:

So the User model I am using does a before_validation call to the method that checks if there is a beta invite. If I move this method call to before_save instead. It works correctly. Is there something i’m over looking. When does factory_girl run the after_build and after_create callbacks in relation to active-record’s before_validation and before_save?

  • 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-22T14:14:12+00:00Added an answer on May 22, 2026 at 2:14 pm

    To me it seems like it just should be able to work, but I have had problems with associations in Factory-girl as well. An approach I like to use in a case like this, if the relations are less evident, is to define a special method, inside your factory as follows:

    def Factory.create_beta_user
      beta_invite = Factory(:beta_invite)
      beta_user = Factory(:user, :email => beta_invite.email)
      beta_user
    end
    

    and to use that in your tests, just write

    Factory.create_beta_user
    

    Hope this helps.

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

Sidebar

Related Questions

I have these models that I'm trying to create factories for using factory_girl. class
I have never used Factories before for the simple reason, I don't understand when
I have a task to create multiple session factories for different Data Sources. One
I have a series of models for which I've defined factories. I also have
I have two models (ModelA and ModelB), and FactoryGirl factories for each. I want
I have two factories ( post_factory.rb , comment_factory.rb ) in separate files. I'd like
I have this design: public interface IFactory<T> { T Create(); T CreateWithSensibleDefaults(); } public
I was wondering how to package the factories that I have in my application.
I have a model not unlike the following: class Bike(models.Model): made_at = models.ForeignKey(Factory) added_on
I have two models books and authors joined by books_authors model. A book has

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.