0

Conceptually I am trying to display a list of users in a search result list and have it say 'add as friend' if the user is not a friend and 'unfriend' if they are a friend.

The basic question I need answered to do this is how to say if an user ID exists in an array. I created a friends array which contains all the id numbers of my friends and need to essentially write;

if user_in_search_results_id = id in the array, unfriend, otherwise add as friend.

the 'if user_in_search_results_id = id anywhere in the array' is where i am stuck.

I have tried .where(user_in_search_results_id == @friends) and that did not work. @friends is friend array. I have also tried the .select method without luck.

Thanks

3 Answers 3

2

I think you're looking for the include? method

user_in_search_results_id.each {|id| @friends.include?(id) ? unfriend(id) : add_as_friend(id)}
Sign up to request clarification or add additional context in comments.

Comments

0

The basic question I need answered to do this is how to say if an user ID exists in an array.

You can do

friends_array.member?(user_id) ? "unfriend" : "add friend"

Comments

0

I would use Enumerable#detect.

@friends.detect { |id| user_in_search_results_id == id } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.