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

Undefined method 'user_friendship' after creating #accept_mutual_friendship_test

I've followed Jason's directions in the Mutual Friendship video but I keep on getting the same error when running the user_friendship_test after creating the mutual_friendship tests:

1) Error: test: #accept_mutual_friendship! should accept the mutual friendship. (UserFriendshipTest): NoMethodError: undefined method user_friendship' for #<User:0x007fb13ecace88> /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activemodel-3.2.12/lib/active_model/attribute_methods.rb:407:inmethod_missing' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.12/lib/active_record/attribute_methods.rb:149:in method_missing' test/unit/user_friendship_test.rb:59:inblock (2 levels) in <class:UserFriendshipTest>' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/shoulda-context-1.1.6/lib/shoulda/context/context.rb:413:in instance_exec' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/shoulda-context-1.1.6/lib/shoulda/context/context.rb:413:inblock in create_test_from_should_hash'

2) Error: test: #mutual_friendship! should correctly find the mutual friendship. (UserFriendshipTest): NoMethodError: undefined method user_friendship' for #<User:0x007fb13f2a6b68> /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activemodel-3.2.12/lib/active_model/attribute_methods.rb:407:inmethod_missing' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.12/lib/active_record/attribute_methods.rb:149:in method_missing' test/unit/user_friendship_test.rb:44:inblock (2 levels) in <class:UserFriendshipTest>' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/shoulda-context-1.1.6/lib/shoulda/context/context.rb:436:in instance_exec' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/shoulda-context-1.1.6/lib/shoulda/context/context.rb:436:inblock in run_current_setup_blocks' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/shoulda-context-1.1.6/lib/shoulda/context/context.rb:434:in each' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/shoulda-context-1.1.6/lib/shoulda/context/context.rb:434:inrun_current_setup_blocks' /usr/local/rvm/gems/ruby-1.9.3-p448/gems/shoulda-context-1.1.6/lib/shoulda/context/context.rb:411:in `block in create_test_from_should_hash'

This is very odd to me since I am not calling the "user_friendship" method - I'm supposed to be calling the accept_mutual_friendship! and mutual_friendship methods.

user_friendship_test.rb and user_friendship.rb to follow

3 Answers

user_friendship.rb

def mutual_friendship
    self.class.where( { user_id: friend_id, friend_id: user_id} ).first
 end

def accept_mutual_friendship!
  # Grab the mutual_friendship and update the state without using
  # the state_machine as to not invoke callbacks.
  mutual_friendship.update_attribute(:state, 'accepted')
end

user_friendship_test.rb

context "#mutual_friendship!" do
    setup do
      UserFriendship.request users(:andrew), users(:bruce)
      friendship1 = users(:andrew).user_friendship.where(friend_id: users(:bruce).id).first
      friendship2 = users(:bruce).user_friendship.where(friend_id: users(:andrew).id).first
    end

    should "correctly find the mutual friendship" do
      assert_equal @friendship2, @friendship1.mutual_friendship
    end
  end

  context "#accept_mutual_friendship!" do
    setup do
     UserFriendship.request users(:andrew), users(:bruce)
    end

    should "accept the mutual friendship" do
      friendship1 = users(:andrew).user_friendship.where(friend_id: users(:bruce).id).first
      friendship2 = users(:bruce).user_friendship.where(friend_id: users(:andrew).id).first

      friendship1.accept_mutual_friendship!
      friendship2.reload
      assert_equal 'accepted', friendship2.state
    end
  end

Any help would be appreciated!

Never mind - figured it out:

should be:

friendship1 = users(:andrew).user_friendships.where(friend_id: users(:bruce).id).first
friendship2 = users(:bruce).user_friendships.where(friend_id: users(:andrew).id).first

where user_friendships should replace user_friendship. I also found a few other syntax errors. Do not as I do!