|
Now, in the User class (app/model/user.rb), we have to first connect each user model to its associated relationships:- has_many :follower_relationships, classname: "Relationship", foreign_key: "followed_id"
- has_many :followed_relationships, classname: "Relationship", foreign_key: "follower_id"
复制代码 We need to create two associations, because we have two sets of relationships per user: all the people following them and all the people they follow. And no, those foreign keys shouldn’t be switched. Thefollower_relationship association is responsible for all of your followers. Hence, it needs thefollowed_id foreign key.
Then, we can use those relationships to get to the followers on the other side of them:- has_many :followers, through: :follower_relationships
- has_many :followeds, through: :followed_relationships
复制代码 |
|