In this episode, we dive into how to get all friends for a user in our app. We start by looking at how friendships work in our database — specifically, how users can add each other and how friend relationships are tracked on both sides. We talk through some examples using three users: how friend requests and acceptances turn into established friendships, and what it means if a friend request is pending.
To make all this logic easy to test, we write some unit tests that check whether users have the correct number of friends based on the friend requests sent and accepted. We walk through the process of adding a proper friends
property to the user model, merging two key relationships ("accepted friends of mine" and "accepted friends of") to get a full list of friends, no matter who initiated the request.
As we go, we update and run our tests to ensure everything is working, even expanding the setup to include a fourth user to handle more complex cases. By the end, we've got a robust system for fetching a user's full list of friends, ready for whatever the UI might throw at it. Next up, we'll look at how to remove a friend, so stay tuned!