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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:04:33+00:00 2026-05-13T11:04:33+00:00

I have an issue where I have a parent model Foo , which both

  • 0

I have an issue where I have a parent model Foo, which both has_many :bars and has_many :bazes. Finally, I also have a join model BarBaz which belongs_to :bar and belongs_to :baz. I want to validate all bar_bazes so that its bar and baz both belong to the same foo. But I can’t seem to figure out a way to define a factory for this model that would be valid.

Factory.define(:bar) do |bar|
  bar.association(:foo)
end

Factory.define(:baz) do |baz|
  bar.association(:foo)
end

Factory.define(:bar_baz) do |bar_baz|
  baz_bar.association(:foo)
  baz_bar.association(:bar)
  baz_bar.association(:baz)
end

I get an invalid record error when I try to create the latter, because the bar and baz factory_girl tries to associate it each have their own foo. Am I screwed?

  • 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-13T11:04:33+00:00Added an answer on May 13, 2026 at 11:04 am

    So after hours of beating my head against this issue, I think I finally have a solution. It’s pretty insane though, so hopefully someone else can still show me where I’m being stupid.

    Factory.define :foo do |foo|
    end
    
    Factory.define :bar do |bar|
    end
    
    Factory.define :baz do |baz|
    end
    
    Factory.define :foo_with_baz do |foo|
      foo.after_create { |foo| Factory(:baz, :foo => foo) }
    end
    
    Factory.define :bar_baz do |bar_baz|
      bar_baz.bar {|bar| bar.association(:bar, :foo => Factory(:foo_with_baz))
      bar_baz.after_build {| bar_baz| bar_baz.baz_id = bar_baz.foo.bars.first.id }
    end
    

    The key issue being that there needs to be a foo in the database already you can get at through the factories alone, since you can use local variables or arbitrary ruby code in factories.rb (as far as I can tell).

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

Sidebar

Ask A Question

Stats

  • Questions 300k
  • Answers 300k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer How to fetch date ranges that intersect with another range… May 13, 2026 at 8:00 pm
  • Editorial Team
    Editorial Team added an answer You need the command reshape: Say your initial matrix is… May 13, 2026 at 7:59 pm
  • Editorial Team
    Editorial Team added an answer Okay, i've done this myself, using JSCompressor by Michael Palmer… May 13, 2026 at 7:59 pm

Related Questions

Here's the deal: I have an Android application that needs to call a web
I'm working with an unchangeable legacy database schema where each instance of an object
I'm having a minor css issue. I have a series of layered divs and
Ok, I have a question relating to an issue I've previously had. I know
I have an odd linq subquery issue. Given the following data structure: Parents Children

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.