Ruby On Rails Classroom image

Pooja  Negi / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4|Last
Lessons:- A Problem with the Data Model (and a Solution)

 

Upon reflection, we see that in these cases the application should either create or destroy a relationship between two users. A user then has_many :relationships, and has many followed_users (or followers) through these relationships. Indeed, Figure 11.6 already contains most of the implementation: Since each followed user is uniquely identified by followed_id, we could convert followed_users to a relationships table, omit the user details and use followed_id to retrieve the followed user from the users table. Moreover, by considering reverse relationships, we could use the follower_id column to extract an array of user’s followers.

           To make a followed_users array of users, it would be possible to pull out an array of followed_id attributes and then find the user for each one. As you might expect, though, Rails has a way to make this procedure more convenient, and the relevant technique is known as has_many through. As we will see in Section 11.1.4, Rails allows us to say that a user is following many users through the relationships table, using the succinct code

 

has many  :followed users, through: :relationships, source:  "followed id"

 

This code automatically populates user.followed_users with an array of followed users. A diagram of the data model appears in Figure 11.7.  

 

 
 
 
image
Pooja   Negi

Skills    Ruby On Rails

Qualifications :- High School - SSN high school, College/University - HNBGU, College/University - SRHU,
Location :-Ranipokhari,Rishikesh,Uttarakhand,India
Description:- Student
Explore
 

  Students (0)