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

Should 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.

Brandon Barrette
Brandon Barrette
20,485 Points

can you paste in your code from the test and the model (then i can compare it with mine)

user_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
Brandon Barrette
20,485 Points

So 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

Library > 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
Brandon Barrette
20,485 Points

To 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.

Ok. I'll check it out. Thanks!

Ok, 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
Brandon Barrette
20,485 Points

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

Ok. Awesome. Thank you!

I reset my entire database, and now I get three errors all saying that it can't find the table 'statuses'

2 Answers

Figured it out. Just had duplicates of files in my migrate folder

Jason Sigmon
Jason Sigmon
9,776 Points

Charles Tobias I'm having a similar issue. How did you find the duplicate files?