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 trialTony McKeown
Courses Plus Student 14,300 PointsTreebook - Create mutual friends lists
Hello,
I've created a friends tab on the user profile page and would like to add a separate bit that shows friends that you have in common with the profile that you are viewing.
I've tried lots of different ways to compare the friends list of the current user and profile page user to find common friends that you have but all the MANY different ways I've tried haven't returned anything. Not even an error message. Can anyone give me a hint on how to construct the query as I'm a little stuck!
Edit - a quick example - I think I got this to work but is there a better way?
<% if current_user.friends.include?(friend) and @user.friends.include?(friend) %>
<%= friend.profile_name %>
<% end %>
But how would I write the query in the profiles_controller so that I can use it in a do block to get additional information in a nicely formatted way?
1 Answer
David Rhinehart
2,268 Pointsyou could create a new array of common elements using &
common_friends = current_user.friends & @user.friends
Tony McKeown
Courses Plus Student 14,300 PointsTony McKeown
Courses Plus Student 14,300 PointsThanks David. I can't believe how simple that was. I created a long convoluted way myself by overthinking the problem! At least I can now cut down the code that I was using.