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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:39:53+00:00 2026-05-23T03:39:53+00:00

I have an Account model that has_one User model, and a User model that

  • 0

I have an Account model that has_one User model, and a User model that belongs_to Account model. I think that the basic code required for demonstration is:

class Account < ActiveRecord::Base
  has_one :user
  validates_presence_of :user
  accepts_nested_attributes_for :user
end

class User < ActiveRecord::Base
  belongs_to :account
  # validates_presence_of :account # this is not actually present,
                                   # but is implied by a not null requirement
                                   # in the database, so it only takes effect on
                                   # save or update, instead of on #valid?
end

When I define associations in each factory:

Factory.define :user do |f|
  f.association :account
end

Factory.define :account do |f|
  f.association :user
end

I get a stack overflow, as each is creating an account/user recursively.

The way I’ve been able to solve this is to emulate nested attribute forms in my tests:

before :each do
  account_attributes = Factory.attributes_for :account
  account_attributes[:user_attributes] = Factory.attributes_for :user
  @account = Account.new(account_attributes)
end

However, I’d like to keep this logic in the factory, as it can get out of hand once I start adding other modules:

before :each do
  account_attributes = Factory.attributes_for :account
  account_attributes[:user_attributes] = Factory.attributes_for :user
  account_attributes[:user_attributes][:profile_attributes] = Factory.attributes_for :profile
  account_attributes[:payment_profile_attributes] = Factory.attributes_for :payment_profile
  account_attributes[:subscription_attributes] = Factory.attributes_for :subscription
  @account = Account.new(account_attributes)
end

Please help!

  • 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-23T03:39:54+00:00Added an answer on May 23, 2026 at 3:39 am

    I was able to solve this problem by using factory_girl’s after_build callback.

    Factory.define :account do |f|
      f.after_build do |account|
        account.user ||= Factory.build(:user, :account => account)
        account.payment_profile ||= Factory.build(:payment_profile, :account => account)
        account.subscription ||= Factory.build(:subscription, :account => account)
      end
    end
    
    Factory.define :user do |f|
      f.after_build do |user|
        user.account ||= Factory.build(:account, :user => user)
        user.profile ||= Factory.build(:profile, :user => user)
      end
    end
    

    This will create the associated classes before the owning class is saved, so validations pass.

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

Sidebar

Related Questions

I have an Account model and a User model: class Account < ActiveRecord::Base has_many
I have a program that creates a Windows user account using the NetUserAdd() API
I believe that I have successfully impersonated my own user account while running an
I have an UIViewController subclass that allows the user to add a new model
I have the following models: class Instance < ActiveRecord::Base has_many :users has_many :books end
Say that I have two models- Users and Accounts. Each account can have at
when calling a SSIS package (C# app) using LoadFromSqlServer, does the user account have
I have a table that saves some account limits like users. For most rows
I have a need to display a UITableView containing a user's account credentials. For
I have a View class (OrderView.aspx) which shows the details of an order (Account

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.