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 Mutual Friendship

Why isn't the friendship.count changing by 2?

In the first few minutes of this video a controller test is set up to make sure the relationship count changes by 2. Mine isn't and I'm trying to work out why. Any ideas?

relationship_controller_test:

test "should successfully create 2 user relationship objects" do
    assert_difference 'Relationship.count', 2 do
      post :create, relationship: { followed_id: users(:firstname).name }
    end
  end

relationship_controller:

def create
    if params[:relationship] && params[:relationship].has_key?(:followed_id)
      # @followed = User.find(params[:relationship][:followed_id])
      @followed = User.where(name: params[:relationship][:followed_id]).first
      @relationship = Relationship.request(current_user, @followed)
      if @relationship.new_record?
        flash[:danger] = "There was a problem creating that relationship request"
      else
        flash[:success] = "Friend request sent"
      end
      redirect_to followed_path(@followed)
    else
      flash[:danger] = "Friend Required"
      redirect_to users_path
    end
  end

model/relationship:

# makes sure 2 relationships are created otherwise none are created. 
  def self.request(user1, user2)
    transaction do
      relationship1 = create!(follower: user1, followed: user2, state: 'pending')
      relationship2 = create!(follower: user2, followed: user1, state: 'requested')

      relationship1.send_request_email
      relationship1
    end
  end

I'm not sure if there's somewhere else I should be looking too?

Greatly appreciate any help working this one out!

1 Answer

Brandon Barrette
Brandon Barrette
20,485 Points

What I would do is type this in your console:

tail -f log/test.log

This will show you what's happening on the test. Then run just that test (make sure you add the line number for the test) like if the test was on line 9:

bin/rspec spec/controllers/user_friendships_controller.rb:9

(Note this is the path for my files, not necessarily yours)

Then watch the test and see if it is creating new friendships or if something is going wrong. I need a bit more information from you in order to help because I'm not really sure what you're seeing.