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 Building Social Features in Ruby on Rails Adding State What is a State Machine?

Nelly Nelly
Nelly Nelly
7,134 Points

Failures in user_friendship_test.rb

Hello I am having troubles when running this test.....

 1) Failure:
UserFriendshipTest#test_: a new instance should have a pending state.  [test/models/user_friendship_test.rb:24]:
Expected: "pending"
  Actual: nil


  2) Failure:
UserFriendshipTest#test_that_creating_a_friendship_based_on_user_id_and_friend_id_works [test/models/user_friendship_test.rb:15]:
Expected false to be truthy.

5 runs, 4 assertions, 2 failures, 0 errors, 0 skips

My user_friendships.rb model

class UserFriendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, class_name: 'User', foreign_key: "friend_id"

  state_machine :state, initial: :pending do
  end
end

user_friendship_test.rb

require 'test_helper'

class UserFriendshipTest < ActiveSupport::TestCase
  should belong_to(:user)
  should belong_to(:friend)

  test "that creating a friendship works without raising an exception" do
    assert_nothing_raised do
      UserFriendship.create user: users(:jason), friend: users(:mike)
    end
  end

  test "that creating a friendship based on user id and friend id works" do
    UserFriendship.create user_id: users(:jason).id, friend_id: users(:mike).id
    assert users(:jason).friends.include?(users(:mike))
  end

  context "a new instance" do
    setup do
      user_friendship = UserFriendship.new user: users(:jason), friend: users(:mike)
    end

    should "have a pending state" do
      assert_equal 'pending', @user_friendship.state
    end
  end
end

edit adding my controller

class UserFriendshipsController < ApplicationController
  before_filter :authenticate_user!, only: [:new]


  def new
    if params[:friend_id]
      @friend = User.where(profile_name: params[:friend_id]).first
      raise ActiveRecord::RecordNotFound if @friend.nil?
      @user_friendship = current_user.user_friendships.new(friend: @friend)
    else
      flash[:error] ="Friend requiered"
    end
  rescue ActiveRecord::RecordNotFound
    render file: 'public/404', status: :not_found
  end

 def create
   if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
     @friend = User.where(profile_name: params[:user_friendship][:friend_id]).first
     @user_friendship = current_user.user_friendships.new(friend: @friend)
     @user_friendship.save
     flash[:success] = "You are now friends with #{@friend.full_name}"
     redirect_to profile_path(@friend)
   else
     flash[:error] = "Friend requiered"
     redirect_to root_path
   end
 end


  private
  def params_user_friendship
    params.require(:friendship).permit(:user, :friend, :user_id, :friend_id)
  end
end

Steve Hunter maybe ??

Hiya!

Don't we need to see what happens in the controller code regarding the pending state?

Is this in your Github repo? Sorry, I didn't get chance to look at your other problem today - does that still cause a problem?

Steve.

Nelly Nelly
Nelly Nelly
7,134 Points

Steve Hunter I edited my initial post with the controller... I believe I skipped something??? (maybe I also need to sleep lol) And nop it's not on github yet I left the other problems for this course, maybe it could help too :)

1 Answer

Nelly Nelly
Nelly Nelly
7,134 Points

Got it ! it was gem "state_machines" I dropped the machine's'

Good work! :+1: :wink: