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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:33:44+00:00 2026-05-24T12:33:44+00:00

Trying something simple and overlooking the obvious I’m sure. Shouldn’t Factory_Girl be automatically creating

  • 0

Trying something simple and overlooking the obvious I’m sure. Shouldn’t Factory_Girl be automatically creating the association? If so why does “GET index” spec below fail because event_id is nil?

Event has_many :posts

#post.rb
  ...
  belongs_to :event
  validates :event_id, :presence => true
  ...

#factories.rb
Factory.define :event do |event|
  event.name "The Long Event Title"
end

Factory.define :post do |post|
  post.title "This is a post"
  post.association :event
end


#posts_controller_spec.rb
before(:each) do
  @attr = Factory.attributes_for(:post)
end

describe "GET index" do
  it "assigns all posts as @posts" do
    post = @user.posts.create(@attr)    ### <-- event_id not assigned?
    # post = FactoryGirl.create(:post)  ### <-- this doesn't work either
    get :index
    assigns(:posts).should eq([post])
  end
end

Edit: Additional spec example:

describe "POST create" do
    describe "with valid params" do
      it "creates a new Post" do
        expect {
          post :create, :post => @attr, :event => Factory.create(:event)  <- fail
        }.to change(Post, :count).by(1)
      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-05-24T12:33:44+00:00Added an answer on May 24, 2026 at 12:33 pm

    From the FactoryGirl wiki: https://github.com/thoughtbot/factory_girl/wiki/Usage

    # Attribute hash (ignores associations)
    user_attributes = Factory.attributes_for(:user)
    

    So you are not getting an event_id, and that’s why it’s failing.

    Also, you said you tried post = FactoryGirl.create(:post) but you should do post = Factory.create(:post) and that will get it to work.

    Perhaps in your before() block you should just create and save the post, unless you have a test that requires it to not be saved yet.

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

Sidebar

Related Questions

I am probably overlooking something really simple here but I am trying to redirect
I am trying something very simple, but for some reason it does not work.
actually I thought I was trying something really simple. ControllerClassNameHandlerMapping sounded great to produce
I feel like I'm trying to do something simple but I am not getting
Hey guys, I've been trying to build something simple in Flash 8, and I
I'm trying to do something very simple, yet I can't seem to find the
I'm trying to do something seemingly simple: ensure that Application_BeginRequest is called for every
I am trying to accomplish something very simple, and I'm hoping someone can point
I'm trying to do something really simple, but starting to realize that dates in
I am trying to deserialize an object from XML in Ruby. Something simple like:

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.