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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:41:30+00:00 2026-06-03T05:41:30+00:00

I want to pass a parameter into a factory that will be used to

  • 0

I want to pass a parameter into a factory that will be used to set the attribute on an associated model. The associated model is created within the factory.

I have a Transaction model with a many-to-many link to itself through a join table TxLink.
I want to call link = FactoryGirl.create(:link_red_to_sub, sub: 10, red: 7) which will create two Transaction objects and a TxLink that links the two.

I get an error in the association line below because of the units: sub at the end. The error is “trait not defined”. I tried units: { sub } (with brackets) instead but no dice.

factory :tx_link do
    units "9.99"

    factory :link_red_to_sub do
      ignore do
        sub 0
        red 0
      end
      units { red }

      association :giver, factory: :transaction, units: sub
      association :taker, factory: :redemption, units: red
    end
  end
  • 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-03T05:41:31+00:00Added an answer on June 3, 2026 at 5:41 am

    The association helper in factory_girl can’t take dynamic property arguments, but you can use a dynamic attribute to instantiate the association however you want:

    factory :tx_link do
      units "9.99"
    
      factory :link_red_to_sub do
        transient do
          sub 0
          red 0
        end
        units { red }
    
        giver { create(:transaction, units: sub) }
        taker { create(:redemption, units: red) }
      end
    end
    

    The only downside of this is that the giver and taker associations will use the create strategy regardless of what strategy you’re building tx_link with, which essentially means that attributes_for and build_stubbed won’t work correctly for the tx_link factory.

    The example I provided requires version 4.5.0.

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

Sidebar

Related Questions

I want to pass a parameter into my function. The parameter will be a
i have this jquery function that i want to pass topic parameter to, i
I have following code and i want to pass additional parameter in JSON.getJSONfromURL() method
I have a NameValueCollection with parameter. I want to pass all parameter in SqlCommand
I have this class which I want to pass around Windows as LPARAM parameter.
Haskell beginner here. I want to pass a type parameter with JSON, and have
I have a Javascript object that I want to pass in more parameters, but
I have a collection IQueryable<Car> and I want to pass it into a generic
I'm traversing an object graph and want to pass it a block that will
I have an xml document which i want to pass as a parameter to

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.