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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:11:57+00:00 2026-05-28T08:11:57+00:00

I defined these 3 models in Rails3. class User < ActiveRecord::Base has_many :questions has_many

  • 0

I defined these 3 models in Rails3.

class User < ActiveRecord::Base
  has_many :questions
  has_many :answers

class Question < ActiveRecord::Base
  belongs_to :user
  has_many :answers

class Answer < ActiveRecord::Base
  belongs_to :user
  belongs_to :question

I wrote RSpec like this:

describe "user associations" do
  before :each do
    @answer = @user.answers.build question: @question
  end

  it "should have the right associated user" do
    @answer.user.should_not be_nil
  end

  it "should have the right associated question" do
    @question.should_not be_nil
    @answer.question.should_not be_nil #FAIL!!
  end

But I always get the following error:

Failures:

  1) Answer user associations should have the right associated question
     Failure/Error: @answer.question.should_not be_nil
       expected: not nil
            got: nil

I guess this line is wrong:

@answer = @user.answers.build question: @question

But how should I build answer object?

Update: Thanks everyone, I found I should have to write like this:

require 'spec_helper'

describe Answer do
  before :each do
    @user = Factory :user 
    asker = Factory :user, :user_name => 'someone'
    @question = Factory :question, :user => asker
  end

  describe "user associations" do
    before :each do
      @answer = Factory :answer, :user => @user, :question => @question
    end

    it "should have the right associated user" do
      @answer.user.should_not be_nil
    end

    it "should have the right associated question" do
      @answer.question.should_not be_nil
    end
  end
end

Here is spec/factories.rb:

Factory.define :user do |user|
  user.user_name "junichiito"
end

Factory.define :question do |question|
  question.title "my question"
  question.content "How old are you?"
  question.association :user
end

Factory.define :answer do |answer|
  answer.content "I am thirteen."
  answer.association :user
  answer.association :question
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-28T08:11:57+00:00Added an answer on May 28, 2026 at 8:11 am

    Once I explicitly save the @user instance, the spec doesn’t fail anymore. Here’s my version:

    require 'spec_helper'
    
    describe Answer do
      before :each do
        @user = User.new
        @user.save!
        @question = @user.questions.build 
        @question.save!
    
        @answer = @user.answers.build question: @question
        @question.answers << @answer
      end
    
      it "should have the right associated user" do
        @answer.user.should_not be_nil
      end
    
      it "should have the right associated question" do
        @question.should_not be_nil
        @answer.question.should_not be_nil # SUCCESS!
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I have now: class User < ActiveRecord::Base has_many :people end ... and... class
I have some logic, which defines and uses some user-defined types, like these: class
In my rails app, i am using a legacy database. class Expression < ActiveRecord::Base
I have a ActiveRecord Car model: class Car < ActiveRecord::Base def check_value puts self.load_size
I have defined a custom file type with these lines: syn region SubSubtitle start=+=+
I have these 3 file in my program: sample1.h (method in sample1.cpp are defined
Let us suppose i have these three methods defined: int F1(int, int); int F1(float,
I've some classes defined in a dll file. These are in the form of
I've implemented some extension methods as defined here . I've put these into a
Is there an existing gem for adding user-defined field functionality to an existing Ruby

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.