Following the Path to Success
Published: 2021-03-26 18:03:57

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/od87gb9p6z6uefriur1f.png)
 
 
Two months of the program are officially in the books and I cannot be prouder! Phase 2 was a lot of information but it really all came together once I got this project put together. I decided to make an application that would allow you to input predictions for upcoming games in the English Premier League. A lot of aspects of this project were learning experiences and were difficult to build out, but the following feature in my program was definitely my favorite and most challenging piece.

I wanted to create a User model that could have many relationships with other users. In that sense I wanted my Users to not only be able to follow other users, but also to be a followed by other users. After a ton of googling and speaking with my cohort leads I realized a model of Follow that could be aliased for a multidirectional relationship would be the best course of action for this program. ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w6t64n02plnspul8djuz.png)
Above illustrates my User model where I set up a relationship where User can have many follows in line 9. Then in line 11 and 12 outline we are saying there is a table that ActiveRecord is going to make up called Follower_Relationships, through this a foreign key of following_id is set. The next line then aliases this key as follower so that we can call followers on a User to see who follows them. The same thing happens for lines 14 and 15 however it is just in the opposite direction. 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7is617i99s94op6mu6g.png)

Next we set the belongs to relationship in the Follow class to establish these aliases as the belong to relationship. No when we call the follower or following methods on our User class it will tell you who is a follower or following correctly, based on the direction of the relationship (which is based off of two user ID's). This is a pretty complex relationship as it using a has many relationship with the Follow class but what we are actually accomplishing is using Follow as a placeholder for ID's so we can refer back and see which users have this relationship.

Now that we have this relationship we can also set up a pretty useful helper method called following?.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ld8vdy4bkv4olc44n99j.png)

This allows us to set certain conditions across the program that make features inaccessible if other users are not following the user they are trying to access. For example, in my program other users cannot see score predictions from other users unless they are following them. 

All in all, this project was great experience and this is clean easy code that should be widely repeatable in many future programs. To go with that, I hope to build this project out further so that we can pull data from an API or scrape it to pull the actual team matchups for the EPL weekend games as well as follow requests so users need permission to follow another user. 

If I could give any advice to another programmer going through this course is to hang in there, once you see the big picture all of the things you learned will make more sense. Additionally, if anyone needed to create this relationship this is a great model to use.