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!
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

Charles Tobias
3,690 PointsShould belong_to(:user) test still fails
So, in the Testing Friendships video of the Creating Friendships lesson, when I test "should belong_to(:user)" and "should belong_to(:friend)" the tests fail with the failure: "Expected UserFriendship to have a belongs_to association called user (no association called user)" like in the video, but when I add "belongs_to :user" like they tell me to, I still get a failure, but this time, it is: "Expected UserFriendship to have a belongs_to association called user (UserFriendship does not have a user_id foreign key.)" Can anyone help me?
The change in the failure is in the parentheses.

Charles Tobias
3,690 Pointsuser_friendship_test.rb: require 'test_helper'
class UserFriendshipTest < ActiveSupport::TestCase
should belong_to(:user)
should belong_to(:friend)
end
user_friendship.rb:
class UserFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend
end

Brandon Barrette
20,485 PointsSo in my user_friendship.rb I have
belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'
granted, that may come in a later video. What video are you on. Can you give me the specific link, like:
Library > Programming > __?? > ____?? etc

Charles Tobias
3,690 PointsLibrary > Programming > Building Social Features in Ruby on Rails > Creating Friendships > Testing Friendships, and the part I'm talking about it from 2:05 - 3:55. Where his test passes, I get the "Expected UserFriendship to have a belongs_to association called user (UserFriendship does not have a user_id foreign key.)" failure message.
The whole failure message is:
1) Failure: test: UserFriendship should belong to user. (UserFriendshipTest) [/usr/local/rvm/gems/ruby-1.9.3-p392/gems/shoulda-context-1.1.4/lib/shoulda/context/context.rb:339]: Expected UserFriendship to have a belongs_to association called user (UserFriendship does not have a user_id foreign key.)

Brandon Barrette
20,485 PointsTo me sounds like your database doesn't have something right. Under that video, you can download the source files and compare your migration file with the migration Jason did in the video. Sometimes it's something as simple as a pluralized word or a missing colon. This part of the error: "UserFriendship does not have a user_id foreign key." is what seems to be key here.
Unfortunately I don't have enough experience to exactly pinpoint your error.

Charles Tobias
3,690 PointsOk. I'll check it out. Thanks!

Charles Tobias
3,690 PointsOk, so when I try to run rake db:migrate, I get this error:
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "user_friendships" already exists: CREATE TABLE "user_friendships" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "friend_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /usr/local/rvm/gems/ruby-1.9.3-p392/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `initialize'

Brandon Barrette
20,485 Pointsyou can run rake db:setup and it will redo your database, but you will lose anything you had stored in there.

Charles Tobias
3,690 PointsOk. Awesome. Thank you!

Charles Tobias
3,690 PointsI reset my entire database, and now I get three errors all saying that it can't find the table 'statuses'
2 Answers

Charles Tobias
3,690 PointsFigured it out. Just had duplicates of files in my migrate folder

Jason Sigmon
9,776 PointsCharles Tobias I'm having a similar issue. How did you find the duplicate files?
Brandon Barrette
20,485 PointsBrandon Barrette
20,485 Pointscan you paste in your code from the test and the model (then i can compare it with mine)