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

  • Home
  • SEARCH
  • 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 8426621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:34:45+00:00 2026-06-10T04:34:45+00:00

Given two models, Alert and Zipcode, where one Alert must have 1 or more

  • 0

Given two models, Alert and Zipcode, where one Alert must have 1 or more Zipcodes:

class Alert < ActiveRecord::Base
  attr_accessible :descr, :zipcode

  has_many :zipcode
  validates :zipcode, :length => { :minimum => 1 }
end

class Zipcode < ActiveRecord::Base
  attr_accessible :zip
  belongs_to :alert
end 

How do I write my FactoryBot factories so that:

  • Zipcode factories are defined in their own file
  • Alert factories are defined in their own file
  • Alert can rely on the factory defined by Zipcode?

All of the documentation and examples I read expect you to define the contained class inside the parent factory file, blob them all together, or make some other compromise or work-around. Isn’t there a clean way to keep the spec factories separate?

  • 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-06-10T04:34:46+00:00Added an answer on June 10, 2026 at 4:34 am

    The trick is to make sure the container class, that is, the one with a has_many statement in its definition, creates the contained class as an array in FactoryBot. For example:

    In your spec/factories/zipcodes.rb:

    FactoryBot.define do
      factory :zipcode do
        zip { 78701 + rand(99) } 
      end
    end
    

    And in spec/factories/alerts.rb:

    FactoryBot.define do
      factory :alert do
        zipcode { Array.new(3) { FactoryBot.build(:zipcode) } }
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models: class Group < ActiveRecord::Base belongs_to :sites end class Site <
Given two models and a controller: Apples class Apples < ActiveRecord::Base belongs_to :not_oranges ...
I have the following models in my Rails application: class Shift < ActiveRecord::Base has_many
I have three models linked by foreign keys: class One(models.Model): ... class Two(models.Model): one
Given these two models: class Profile(models.Model): user = models.ForeignKey(User, unique=True, verbose_name=_('user')) about = models.TextField(_('about'),
Given the following two models: class Card(models.Model): disabled = models.BooleanField(default=False) class User(models.Model): owned_cards =
I have these two models: class CommonVehicle(models.Model): year = models.ForeignKey(Year) series = models.ForeignKey(Series) engine
I have the following two models class Author(Model): name = CharField() class Publication(Model): title
if I have two simple models: class Tag(models.Model): name = models.CharField(max_length=100) class Post(models.Model): title
I have two Models, one that has the name Exam and one that 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.