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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:06:31+00:00 2026-06-13T06:06:31+00:00

I kind of have a chicken and egg type problem. I have an Isp

  • 0

I kind of have a chicken and egg type problem. I have an Isp model that has a default_domain _id, that I need to populate for my tests. The problem is Domain belongs to the ISP. So I am trying to figure out how to build the factory to generate an ISP

factories/isp.rb

FactoryGirl.define do
  factory :isp do
    sequence :name do |n|
      "ISP" + n.to_s
    end
  end
end

factories/domain.rb

FactoryGirl.define do
  factory :domain do
    isp
    account
    sequence :dn do |n|
      "foo-#{n}.com"
    end
    active :true
  end
end

app/models/isp.rb

class Isp < ActiveRecord::Base
  has_many :domains
  belongs_to :default_domain, class_name: 'Domain'
 end

app/models/domain.rb

class Domain < ActiveRecord::Base
  belongs_to :isp
  ...
 end

I’ve tried using after(:create) callbacks in the factory, that just seems to create circular reference. Tried using a block to lazily evaluate it. Same thing. Just at a loss where to look next or what I should try..

  • 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-13T06:06:32+00:00Added an answer on June 13, 2026 at 6:06 am

    It appears you found a solution. However, if you want to utilize factories to create the default_domain attribute, try this.

    First, If you change your domain factory so that it does not reference the isp factory, you’ll avoid the circular reference.

    Second, in your isp factory definition, you can do this:

    FactoryGirl.define do
      factory :isp do
        sequence(:name) {|n| "ISP#{n}" }
        after_create do |isp|
          isp.default_domain = Domain.find_by_dn("#{isp.name}.com") ||
            FactoryGirl.create(:domain, :dn => "#{isp.name}.com")
          isp.save!
        end
      end
    

    This will let you utilize FactoryGirl for the more complicated attributes of the Domain model, and if you wanted to customize the dn field from outside the factory, you could use transient attributes and utilize them in the after_create hook.

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

Sidebar

Related Questions

I have kind of a weird problem .. Something that isn't working on ff
I have kind of a lazy form that has two textboxes. When I click
I have kind of a problem that I do not know how it happened.
I have a kind of basic (but still unresolved) problem. I'm building a program
I have some kind of problem with jQuery selectors. Let's say i want to
I have a kind of strange thing that I really nead for my text
I have kind of a settings table where are columns like type, value, company,
I have kind of a tricky Oracle problem. I am trying to select one
I have kind of a unique problem in the project I'm working in. What
I have a rather large legacy Java Applet that has several problems: It looks

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.