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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:22:46+00:00 2026-05-12T08:22:46+00:00

Can anyone suggest a better way to make a factory use a pre-built model

  • 0

Can anyone suggest a better way to make a factory use a pre-built model
instance for its association? For example, so that it would be possible
below to define a child of the Message factory so that a call to
Factory(:my_message) could substitute for
Factory(:message,:sender=>@me) ?

Sometimes the setup hash is more involved than in this contrived
example, or is just repeated in so many tests that it would seem better to push it down into the factory.

One alternative I can think of is to define a test helper method
such as create_message_owned_by(@me), but I’m hoping there’s a way within factory_girl itself.

factory_girl factories:

Factory.define :sender do |s|
  sender.name "Dummy name"
end

Factory.define :message do |msg|
  msg.text "Dummy text"
  msg.association :sender
end

Factory.define :my_message, :parent=>:message do |msg|
  msg.text "Dummy text"
  ### ? what goes here for msg.association :sender ? ###
end

MessagesControllerTest excerpt (using shoulda):

context "on GET /messages" do
  setup do
    @me = Factory(:sender)
    @my_message = Factory(:message,:sender=>@me)
    @somebody_elses_message = Factory(:message)
    sign_in_as(@me)
    get :index
  end
  should "only assign my messages" do
    assert_contains(assigns(:messages), @my_message)
    assert_does_not_contain(assigns(:messages), @somebody_elses_message)
  end
end
  • 1 1 Answer
  • 3 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-12T08:22:46+00:00Added an answer on May 12, 2026 at 8:22 am

    I don’t know if this is what you’re looking for, but if you first create message you can fetch the sender through that and assign it to @me.

    @my_message = Factory(:message)
    @me = @my_message.sender
    

    Does that help at all?

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

Sidebar

Related Questions

I was wondering if you can offer me a better way of achieving the
Trying to figure out a way where I can pass some data/fields from a
This is one of those there's gotta be a better way questions. Let me
I am fairly new to MVC so please don't hesitate to suggest a better/cleaner/simpler
I'm trying to figure out the best way to share an instance of a
What I want is a memoization decorator that: can memoize instance methods with both
I wish Subversion had a better way of moving tags. The only way that
I want to make a function which moves items from one STL list to
It seems like a bit of an omission that there's no easy way to
wonder if I could get some advice on the best way to solve this

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.