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

Destroying Friendships Error

Hello, I'm getting an error I can't quite put my finger on and I've been trying to figure it out for a while today. It's not an error I've seen before, and I seem to run into just about every one out there.

I'm about 4 minutes into the rails adding state: destroying features video and I'm getting this error when running the user_friendship_test.rb

test: #delete_mutual_friendship! should delete the mutual friendship. (UserFrien
dshipTest):
RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wa
nted the id of nil, use object_id
test/unit/user_friendship_test.rb:124:in `block (2 levels) in <class:UserFri
endshipTest>'

Here's the code from the test:

context "#delete_mutual_friendship!" do
setup do
  UserFriendship.request users(:baggins), users(:gandalf)
  @friendship1 = users(:baggins).user_friendships.where(friend_id: users(:gandalf).id).first
  @friendship2 = users(:gandalf).user_friendships.where(friend_id: users(:baggins).id).first
end

should "delete the mutual friendship" do
  assert_equal @friendship2, @friendship1.mutual_friendship
  @friendship1.delete_mutual_friendship!
  assert !UserFriendship.exists?(@friendships2.id)
end
end

 context "on destroy" do
 setup do
  UserFriendship.request users(:baggins), users(:gandalf)
  @friendship1 = users(:baggins).user_friendships.where(friend_id: users(:gandalf).id).first
  @friendship2 = users(:gandalf).user_friendships.where(friend_id: users(:baggins).id).first
 end

should "delete the mutual friendship" do
  @friendship1.destroy
  assert !UserFriendship.exists?(@friendship2.id)
end
end
end

from user_friendship

after_destroy :delete_mutual_friendship!
....
def delete_mutual_friendship!
mutual_friendship.delete
end
end

2 Answers

I changed - assert !UserFriendship.exists?(@friendships2.id)

to - assert !UserFriendship.exists?(@friendships2.object_id)

and it runs without errors, but I have a feeling this isn't what I want.

Right. I see what I did there. @Friendship not @friendships.