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
Drew S
34,034 PointsHelp with Code Challenge: Finding and Building Models
I'm trying to figure out why my code isn't passing.
This is what it tells me to do:
Create a new UserFriendship instance variable set to user_friendship with the user set to the current_user and the friend set to the @friend instance variable.
This is what I have:
class UserFriendshipsController < ApplicationController
def new
if params[:friend_id]
@friend = User.find(params[:friend_id])
@user_friendship = current_user.user_friendships.new(friend: @friend)
else
flash[:error] = "Friend Required"
end
end
end
Any help is appreciative.
5 Answers
Jim Hoskins
Treehouse Guest TeacherHi Andrew!
I went through this code challenge, and it looks like it's asking for something slightly different from the solution you are offering.
The challenge actually wants you to create an instance of UserFriendship directly, and not through an association.
The code offered
@user_friendship = current_user.user_friendships.create(friend: @friend)
Would work in a real application assuming the association was assigned. However, this code challenge seems to not be looking for an association-based solution, so the environment does not include the association.
Try creating a new instance of UserFriendship with the appropriate data.
Nicholas Watson
Courses Plus Student 1,160 PointsLooking at your code, it looks like you didn't save the @user_friendship object.
@user_friendship.save
The new method creates a new user_friendship, but does not save it to the database. If you wish to create the user_friendship without having to call save, use the create method:
@user_friendship = current_user.user_friendships.create(friend: @friend)
Drew S
34,034 PointsNicholas,
Thank you for your reply
I tried using the code:
@user_friendship = current_user.user_friendships.create(friend: @friend)
And still it is not passing.
The error that it is giving me is:
ed091e8c-9403-45e0-9bc9-0700d62ef478.rb:83:in new': undefined methoduser_friendships' for #<User:0x0000000264aa08 @options={:current_user=>true}> (NoMethodError)
Any other suggestions?
Nicholas Watson
Courses Plus Student 1,160 PointsLooks like you don't have the relationships in the models set up correctly. What does your User model look like?
ecp
838 PointsHey @Andrew!
If you're still having trouble would you be able to email a link of the code challenge to me at help@teamtreehouse.com? Also include a screenshot of the code challenge while you're working on it :)
Thanks! I've ping'ed the Teaching team to help out too ^-^