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 Viewing Todo Items: Part 2

umar razzaq
umar razzaq
714 Points

Statement Invalid Error

I get the following error:

SQLite3::SQLException: no such column: todo_items.todo_list_id: SELECT "todo_items".* FROM "todo_items" WHERE "todo_items"."todo_list_id" = ?

For the following piece of code:

<ul class="todo_items"> <% @todo_list.todo_items.each do |todo_item| %> <li><%= todo_item.content %></li> <% end %> </ul>

4 Answers

umar razzaq
umar razzaq
714 Points

The code didn't show up so I'll add it here:

<ul class="todo_items">
  <% @todo_list.todo_items.each do |todo_item| %>
  <li><%= todo_item.content %></li>
  <% end %>
</ul>

Can you list the full error, or is that it? And that code snippet is in todo_items/index.html.erb, right?

umar razzaq
umar razzaq
714 Points

Yep the code is in index.html.erb, I've added a screenshot here:

http://imgur.com/gXKCEAw

Can you post the rest of the code in that file - I can't see why the id is either causing a problem, or present!

Can you also post the contents of your db/schema.rb file, please? I may be barking up the wrong tree here, but the error is SQL-based, in part.

umar razzaq
umar razzaq
714 Points

Ok,

The schema,

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

  create_table "todo_items", force: :cascade do |t|
    t.integer  "todo_lists_id"
    t.string   "content"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
  end

  add_index "todo_items", ["todo_lists_id"], name: "index_todo_items_on_todo_lists_id"

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

end

The code for the page:

Showing Items for:
<h1><%= @todo_list.title %></h1>

<ul class="todo_items">
  <% @todo_list.todo_items.each do |todo_item| %>
  <li><%= todo_item.content %></li>
  <% end %>
</ul>

<%= link_to "New Todo Item" new_todo_list_todo_item_path %>

Aha!!

This:

schema.rb
t.integer  "todo_lists_id"

should be:

schema.rb
t.integer  "todo_list_id"

I think.

A todo_item exists in one todo_list - the relationship is belongs_to :todo_list. Do you have that in your models/todo_item.rb file?

When you migrated the database to create the todo_items table; what did that migration looks like? It should have something like:

migration.rb
class CreateTodoItems < ActiveRecord::Migration
  def change
    create_table :todo_items do |t|
      t.references :todo_list, index: true, foreign_key: true    
.
.  

What's yours got? (note the singular todo_list in that migration)