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 Creating Friendships What is a Join Table?

Gustavo Paiva
Gustavo Paiva
9,279 Points

Couldn't find status id

Hi there!

I have a little problem following this video.

While testing if a user 'should update status for the current user when logged in', got the following error:

*1) Error: StatusesControllerTest#test_should_update_status_for_the_current_user_when_logged_in: ActiveRecord::RecordNotFound: Couldn't find Status with id=980190962 [WHERE "statuses"."user_id" = ?] app/controllers/statuses_controller.rb:45:in update' test/controllers/statuses_controller_test.rb:76:inblock in <class:StatusesControllerTest>'

2) Error: StatusesControllerTest#test_should_update_status_when_logged_in: ActiveRecord::RecordNotFound: Couldn't find Status with id=980190962 [WHERE "statuses"."user_id" = ?] app/controllers/statuses_controller.rb:45:in update' test/controllers/statuses_controller_test.rb:70:inblock in <class:StatusesControllerTest>'

12 tests, 24 assertions, 0 failures, 2 errors, 0 skips*

Here's my statuses_controller.rb (lines 44 to 56):

  def update
    @status = current_user.statuses.find(params[:id])
    params[:status].delete(:user_id) if params[:status].has_key?(:user_id)
    respond_to do |format|
      if @status.update(status_params)
        format.html { redirect_to @status, notice: 'Status was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

Here's my statuses_controller_test.rb:

 test "should update status when logged in" do
    sign_in users(:gustavo)
    put :update, id: @status, status: { content: @status.content }
    assert_redirected_to status_path(assigns(:status))
  end

  test "should update status for the current user when logged in" do
    sign_in users(:gustavo)
    put :update, id: @status, status: { content: @status.content, user_id: users(:paul).id }
    assert_redirected_to status_path(assigns(:status))
    assert_equal assigns(:status).user_id, users(:gustavo).id
  end

At last, my users.yml:

gustavo:
    first_name: "Gustavo"
    last_name: "Paiva"
    email: "gustavorpaiva@gmail.com"
    profile_name: "grpaiva"

paul:
    first_name: "Paul"
    last_name: "McCartney"
    email: "paulmccartney@gmail.com"
    profile_name: "paulpaul"

I'm using Rails 4.0.4, and I've already made some fixtures on treebook's code so it could work perfectly. Should that be the case or am I missing something?

Thanks!

5 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Gustavo Paiva trying changing @status to statuses(:one) and signing in users(:paul) instead in your tests. I think that will fix the problem. There are many different associations here to different objects and that is why it can't be found in the tests.

Lisa Rossiter
Lisa Rossiter
3,630 Points

I watched this video a thousand times before I realised I typed the code wrong watch again and see if you can spot it, this is what I have for those two tests.

test "should create status for a current user when logged in" do
    sign_in users(:john)

    assert_difference('Status.count') do
      post :create, id: @status, status: { content: @status.content, user_id: users(:bob).id }
    end

    assert_redirected_to status_path(assigns(:status))
    assert_equal assigns(:status).user_id, users(:john).id
  end

  test "should update status for a current user when logged in" do
    sign_in users(:john)
    patch :update, id: @status, status: { content: @status.content, user_id: users(:bob).id }

    assert_redirected_to status_path(assigns(:status))
    assert_equal assigns(:status).user_id, users(:john).id
  end
Lisa Rossiter
Lisa Rossiter
3,630 Points

I could also be wrong. Its a good place to start anyway by re-watching the video.

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Gustavo Paiva can you paste your statuses.yml fixture? That may be where the issue is.

Gustavo Paiva
Gustavo Paiva
9,279 Points

Hi Jason! Here's my statuses.yml:

one:
  content: MyText
  user: paul

two:
  content: MyText
  user: gustavo

I really appreciate the help. Thanks!

Mike Mayer
Mike Mayer
2,151 Points

Gustavo Paiva did you ever find a solution to this problem? I'm struggling with the same issue. Any chance you code is on Github?