Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Relationships

Error in terminal: TodoItem does not have a todo_list_id foreign key

I'm all the way through this video and when I do the final terminal command bin/rspec spec/models/todo_list_spec.rb before committing to git, it gives me this error message:

Failure/Error: it { should have_many(:todo_items) } Expected TodoList to have a has_many association called todo_items (TodoItem does not have a todo_list_id foreign key.)

I checked my db/schema.rb file to see if todo_list_id field is in todo_items table and it is, otherwise I thought it would be a migration issue according to this post. Any other ideas what may be going on? Here is a link to my entire codebase as well in case there is a different file you think could be causing this issue.

Here is the contents of my db/schema.rb file for reference:

# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150506203500) do

  create_table "todo_items", force: true do |t|
    t.integer  "todo_list_id"
    t.string   "content"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "todo_items", ["todo_list_id"], name: "index_todo_items_on_todo_list_id"

  create_table "todo_lists", force: true do |t|
    t.string   "title"
    t.text     "description"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

Thanks! Rob

any luck figuring this one out? I have the same issue

2 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

Two things could be wrong:

  1. Make sure in todo_list.rb model has the line: has_many :todo_items
  2. Make sure you've migrated your test database: rake db:migrate RAILS_ENV=test

Do either of those work?

I checked and I do have the has_many :todo_items line.

I did do the migration of test database as well. But just to be safe I did it again and tried the final command prior to commit (bin/rspec spec/models/todo_list_spec.rb) and still got 1 example, 1 failure including the same error message above.