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 trialRobert Ott
Courses Plus Student 4,614 Pointsrpsec fails after editing todo_item_spec.rb
Per the video I edited the todo_item_spec.rb as follows:
require 'spec_helper'
describe TodoItem do
it { should belong_to(:todo_list) }
end
When I ran rspec I got the following message:
Failures:
1) TodoItem should belong to todo_list Failure/Error: it { should belong_to(:todo_list) } Expected TodoItem to have a belongs_to association called todo_list (no association called todo_list) # ./spec/models/todo_item_spec.rb:4:in `block (2 levels) in <top (required)>'
Finished in 0.00893 seconds 1 example, 1 failure
Failed examples:
rspec ./spec/models/todo_item_spec.rb:4 # TodoItem should belong to todo_list
Randomized with seed 14810
I can't figure out the problem.
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14] Rails 4.2.0 rspec 2.99.2 shoulda-matchers 2.7.0
Thanks in advance!
Robert Ott
Courses Plus Student 4,614 PointsHi Tim,
Thanks for your response.
My /app/models/todo_list.rb looks like this:
class TodoList < ActiveRecord::Base
validates :title, presence: true
validates :title, length: { minimum: 3}
validates :description, presence: true
validates :description, length: { minimum: 5}
end
Do I add your suggested code at the end or beginning or does it matter?
Also, if I want to rollback to a previous version of my odot files with git, what is the process for doing so?
Thanks.
Tim Knight
28,888 PointsThat would go at the top of your model. So something like this:
class TodoList < ActiveRecord::Base
belongs_to :todo_list
validates :title, presence: true
validates :title, length: { minimum: 3}
validates :description, presence: true
validates :description, length: { minimum: 5}
end
As for learning more about how to rollback your git commits you might want to check out the Git Basics Course or this article on StackOverflow that discusses it.
5 Answers
Robert Ott
Courses Plus Student 4,614 PointsTim -
Thanks. Your suggestion still doesn't work but I have done some digging and found a different problem that may be contributing to the problem. What I did to try to fix the problem may have made things worse. I apologize for the details that follow. Let me know if I should start a new discussion thread to address this:
When Jason entered this command: bin/rails generate model todo_item todo_list:references content:string
I entered this: (a space between todo_list: -and- references) bin/rails generate model todo_item todo_list: references content:string
That seems to have affected the model that was created. So I git a git reset --hard (to the previous working version: Add spec for deleting to-dos).
After that I reinstalled the 'shoulda-matches' gem, and regenerated the todo_items model. I ran bin/rake db:migrate and got some error messages BUT the complete: bin/rspec spec/models/todo_item_spec.rb ran with no problem.
I realize now that the git reset --hard restores the previous version of my files but it doesn't reset the database.
My question: If I do another git reset --hard to get to the working files I want, how do a reset the database to the same point.
Here are the errors I got when I ran bin/rake db:migrate:
Robert-Otts-MacBook-Pro-4:odot robertott$ bin/rake db:migrate == 20150126230445 CreateTodoItems: migrating ================================== -- create_table(:todo_items) rake aborted! StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "todo_items" already exists: CREATE TABLE "todo_items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "todo_list_id" integer, "content" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /Users/robertott/teamth/project/odot/db/migrate/20150126230445_create_todo_items.rb:3:in change'
-e:1:in
<main>'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "todo_items" already exists: CREATE TABLE "todo_items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "todo_list_id" integer, "content" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
/Users/robertott/teamth/project/odot/db/migrate/20150126230445_create_todo_items.rb:3:in change'
-e:1:in
<main>'
SQLite3::SQLException: table "todo_items" already exists
/Users/robertott/teamth/project/odot/db/migrate/20150126230445_create_todo_items.rb:3:in change'
-e:1:in
<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I also ran bin/rake db:migrate RAILS_ENV=test. This seemed to have run with no problem:
Robert-Otts-MacBook-Pro-4:odot robertott$ bin/rake db:migrate RAILS_ENV=test == 20150126230445 CreateTodoItems: migrating ================================== -- create_table(:todo_items) -> 0.0039s -- add_foreign_key(:todo_items, :todo_lists) -> 0.0000s == 20150126230445 CreateTodoItems: migrated (0.0043s) =========================
Thanks for your thoughts on this.
Tim Knight
28,888 PointsRobert,
Have you tried rolling back the migration?
rake db:rollback
Robert Ott
Courses Plus Student 4,614 PointsThanks Tim.
I didn't know the command to rollback the db. I assume that I should perform the rollback right after I do the git reset --hard?
Robert
Tim Knight
28,888 PointsRobert,
What you'd probably want to do in the future is first rollback your migration and then you can also reverse the generator. So something like this:
rake db:rollback
rails destroy model todo_item
Rarely have I been in a position during development were I needed to roll things back in git. Since you have already and this is just a development/learning project it's probably going to be best that you just reset your database and reload the schema.
Now please note, this will delete any of the data in your database.
Use:
rake db:drop
rake db:setup
Then also run it on your test environment.
rake db:drop RAILS_ENV=test
rake db:setup RAILS_ENV=test
Robert Ott
Courses Plus Student 4,614 PointsTim,
Many kudos! I'm back on track now.
Thanks!
Tim Knight
28,888 PointsGlad to hear it Robert. Good luck with your continued education with Rails.
Tim Knight
28,888 PointsTim Knight
28,888 PointsRobert,
That error is testing for the model association between the list and the list items. Open up your TodoItem model at
/app/models/todo_list.rb
and make sure you have abelongs_to
association setup using the following code.