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

George Offley
PLUS
George Offley
Courses Plus Student 7,386 Points

Issue with Ruby on Rails Code Challenge

I'm having a heck of a time with this code challenge. I just can't seem to figure out what it is that I am doing wrong.

Instructions:

Change the user_friendships finder to be scoped for pending user friendships when there is no list param, and blocked user friendships when the list param is set to "blocked".

Code:

    class UserFriendshipsController < ApplicationController
      # Write your code here
      before_filter :authenticate_user!
      respond_to :html, :json
      def index
        case params[:list]
          when nil
                 @user_friendships = current_user.user_friendships.all
        when 'blocked'
          @user_friendships = current_user.blocked_user_friendship
        end
        respond_with @user_friendships
      end

end

I've been pulling my hair out over it for almost a week now. Any and all help would be very much appreciated. Andrew Martin had it but I could'nt figure out what I was over thinking

https://teamtreehouse.com/forum/ruby-code-challenge-scoped-finders

Thanks.

15 Answers

Andrew Martin
Andrew Martin
17,513 Points

No worries, glad to be of some help :)

Right, so, yeah, as I mentioned before, by putting in the before_filter and respond_to lines before you define the index method, this test will fail. Exactly why, I don't know. But they need to go.

Secondly, your new case method seems to be missing the case statement, maybe I wasn't so clear in my first explanation (sorry I'm awful at explaining things) but, you DO need the case statement, its just in this code challenge when the params[:list] is equal to nil, you need to set this @user_friendships to the pending friends. (Odd I know)

Those are the only two things I see wrong with it, try both of them and let me know how ya do :)

The solution for all of you the codes that you tried is to be more careful when typing.

class UserFriendshipsController < ApplicationController
def index
case params[:list]
  when nil
        @user_friendships = current_user.pending_user_friendships.all
    when 'blocked'
    @user_friendships = current_user.blocked_user_friendships.all
end
end
end
Andrew Martin
Andrew Martin
17,513 Points

Hi George, oh man this one had me pulling my hair out. The trick with this one is to keep things really simple. I'm not sure if its 'allowed' for us to post the answers, so, I'll just heavily imply them instead :P

First off, we have to treat this challenge as if its completely separate from the project we've been working on, in fact where it says '# write your code here' is precisely where code should NOT be put. This sounds nuts, but I've just tried the code which makes the test pass, and then added in the before_filter and respond_to lines.... and the code now fails. What?!

Anyways so yes, only put code in the def index part of the code.

Secondly, the question is a bit nasty in that it asks us to be scoped to pending friendships if there is no list param, where again in the project, we needed it to have the pending param to do that. Odd, I know.

Finally, in the Treebook project we removed the .all at the end of our scoping because of the nature of the code, but in this code challenge, that isn't the case, so, both instances of @user_friendships should have .all at the end.

Hope this helps..... and remember, simple is good :)

George Offley
PLUS
George Offley
Courses Plus Student 7,386 Points

Hey Andrew Martin thanks for you tips, I thought I had it but I the test is still telling me I'm wrong. You don't have to give me the answer but can you take a look at my code and tell me if I'm close?

class UserFriendshipsController < ApplicationController
  # Write your code here

  before_filter :authenticate_user!
  respond_to :html, :json
  def index
   when nil
    @user_friendships = current_user.user_friendships.all
  when 'pending'
    @user_friendships = current_user.pending_user_friendship.all
  when 'blocked'
    @user_friendships = current_user.blocked_user_friendship.all
   end
    respond_with @user_friendships
  end

end

It;s driving me nuts since it's the second to last challenge in the learning adventure. Thanks again for the help!!

George Offley
PLUS
George Offley
Courses Plus Student 7,386 Points

Oh my god that was the most annoyig thing I've ever dealt with in my life.

Thank you soooo much for the help, you have no idea how happy you've made me!!

I'm kind of still banging my head against a wall with this one, though I got stumped at the first task. I've seen answers before posted, so if you could give the first one that'd be appreciated.

Andrew Martin
Andrew Martin
17,513 Points

Hi Patrick, As with George I won't outright give the answer, but I hope this is enough to help. I think the trick to understanding the first task is to look at what is currently happening within the code. At the moment the @user_friendships is currently scoped to all of the user_friendships. To pass, the only thing that needs to change is what @user_friendships is scoped to.... ie

current_user.user_friendships.all

This must be changed to be scoped only to pending user friendships. Good luck :)

Thanks. Sorry for my tone in my previous post. Getting kind of antsy since I've been having issues with the code challenge engine as well. I might have passed the first one before, but forgot several timeouts later.

Andrew Martin
Andrew Martin
17,513 Points

No, no, not at all, I too had problems with the engine, so that coupled with this challenge was very grrrr.

nevermind. i passed it. figured it out. it took a ton of tries because of the timeouts but I Ifeel a lot better now.

Anybody see what is wrong with this code?

class UserFriendshipsController < ApplicationController @user_friendships = current_user.pending_user_friendships.all def index case params[:list] when nil @user_friendships = current_user.pending_user_friendships.all when 'blocked' @user_friendships = current_user.blocked_user_friendships.all end respond_with @user_friendships end

end

I guess I need to know how to cut and paste it without losing the formatting too..

Ryan Drake
Ryan Drake
12,587 Points

Any advice as to where I'm at with the code below? The code engine consistently plays up though :(

 class UserFriendshipsController < ApplicationController
 def index
 case params[:list]
 when nil
   @user_friendships = current_user.pending_user_friendships.all
  when 'blocked'
   @user_friendships = current_user.blocked_user_friendship.all
 end
 respond_with @user_friendships
   end
 end
end

I am stuck on this exercise and I am not sure if there is a site bug...the code below is not passing and seems to time out the server repeatedly. After pushing it through about 5 times, it finally accepted it as the answer http://teamtreehouse.com/library/scoped-finders

Jason Seifer & team - you might want to have a look at this to check for bugs! :)