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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:08:19+00:00 2026-05-28T02:08:19+00:00

I have been trying to figure out what is wrong with my test. First,

  • 0

I have been trying to figure out what is wrong with my test.
First, it works in the console.

The console:

1.9.2-p290 :015 >   a = Allergy.all
  Allergy Load (0.4ms)  SELECT "allergies".* FROM "allergies" 
 => [#<Allergy id: 1, name: "Milk", desc: "Allergic to milk", created_at: "2012-01-08 16:38:55", updated_at: "2012-01-09 11:48:20", patient_id: 1>, #<Allergy id: 2, name: "Blah", desc: "Test", created_at: "2012-01-09 12:20:48", updated_at: "2012-01-09 12:20:48", patient_id: 2>] 
1.9.2-p290 :016 > a[0]
 => #<Allergy id: 1, name: "Milk", desc: "Allergic to milk", created_at: "2012-01-08 16:38:55", updated_at: "2012-01-09 11:48:20", patient_id: 1> 
1.9.2-p290 :017 > a[0].patient.full_name
  Patient Load (0.5ms)  SELECT "patients".* FROM "patients" WHERE "patients"."id" = 1 LIMIT 1
 => "Test Full Name" 
1.9.2-p290 :018 >

Allergy Controller

class AllergiesController < ApplicationController
  # GET /allergies
  # GET /allergies.json
  def index
    @allergies = Allergy.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @allergies }
    end
  end

  # GET /allergies/1
  # GET /allergies/1.json
  def show
    @allergy = Allergy.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @allergy }
    end
  end
  ...

Allergy Model

class Allergy < ActiveRecord::Base
  validates :name, :presence => true
  belongs_to :patient
end

Patient Model

class Patient < ActiveRecord::Base
  validates :first_name, :last_name, :dob, :presence => true
  has_many :allergies

  def age
    now = Time.now.utc.to_date
    now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
  end

  def full_name
    first_name
  end
end

The test:

require 'test_helper'

class AllergiesControllerTest < ActionController::TestCase
  setup do
    @allergy = allergies(:one)
    @allergy.patient = patients(:one)
  end

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:allergies)
  end
  ...

The fixtures:

# For Allergy
one:
  name: MyString
  desc: MyText
  patient_id: 1

two:
  name: MyString
  desc: MyText
  patient_id: 1

#For Patient
one:
  first_name: Jose
  last_name: Rizal
  middle_name: H
  dob: 2009-10-29

two:
  first_name: MyString
  last_name: MyString
  middle_name: MyString
  dob: 1982-02-11

The error:

AllergiesControllerTest:
     PASS should create allergy (0.22s) 
     PASS should destroy allergy (0.01s) 
     PASS should get edit (0.13s) 
    ERROR should get index (0.14s) 
          ActionView::Template::Error: undefined method `full_name' for nil:NilClass
          /Users/wenbert/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/whiny_nil.rb:48:in `method_missing'

     PASS should get new (0.02s) 
     PASS should show allergy (0.02s) 
     PASS should update allergy (0.02s) 

I have checked the logs and no Patient is saved when I run the test. I just get:

--- !ruby/object:Allergy
attributes:
  id: 298486374
  name: MyString
  desc: MyText
  created_at: 2012-01-09 14:28:21.000000000Z
  updated_at: 2012-01-09 14:28:21.000000000Z
  patient_id: 1
  Patient Load (0.2ms)  SELECT "patients".* FROM "patients" WHERE "patients"."id" = 1 LIMIT 1
--- !!null 
...

So, what gives?

Any replies will be greatly appreciated. I have been stuck at this for almost 4 hours now. 🙁

Could it be my fixtures? My Models?

Best Regards,

W

FYI: I am using Ruby on Rails 3.1

  • 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-28T02:08:19+00:00Added an answer on May 28, 2026 at 2:08 am

    Your fixtures have the same names, name them as following

    allergy_one: 
      name: ... 
      patient: patient_one
    
    allergy_two:
      name: ...
    
    patient_one: 
      name: ..
    

    You will notice that I do not use patient_id in the fixtures. You need to reference the patient fixture you want as fixtures use random ids and do not start at 1.

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

Sidebar

Related Questions

I have been banging my head trying to figure out what is going wrong.
I'm new to python and have been trying to figure this out all day.
I have been trying to figure out whats wrong for quite long time yet
I have been trying to figure out a solution but nothing has really presented
I have been trying to figure out a way to tag several methods from
I am fairly new to Emacs and I have been trying to figure out
I have been trying to figure this out for way to long tonight. I
I have been going mad trying to figure out why my scripts weren't working,
I have been pulling my hair out trying to figure out what I can't
I have been pounding my head against a wall trying to figure out what

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.