Ruby On Rails Classroom image

Neeraj  Amoli / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  |1 | 2 | 3|Last
Lessons:- User/Relationship Associations

 User/Relationship Associations

Before implementing followed users and followers, we first need to establish the association between users and relationships. A user has_many relationships, and—since relationships involve two users—a relationship belongs_to both a follower and a followed user.

         As with microposts in Section 10.1.3, we will create new relationships using the user association, with code such as

user.relationships.build ( followed id: ...)

 

We start with some tests, shown in Listing 11.2, which make a relationship variable, checks that it is valid, and ensures that the follower_id isn’t accessible. (If the test for accessible attributes doesn’t fail, be sure that your application.rb has been updated in accordance with Listing 10.6.)  

 

Note that, unlike the tests for the User and Micropost models, which use @user and @micropost, respectively, Listing 11.2 uses let in preference to an instance variable. The differences rarely matter,4 but I consider let to be cleaner than using an instance variable. We originally used instance variables both because instance variables are important to introduce early and because let is a little more advanced.

      We should also test the User model for a relationships attribute, as shown in Listing 11.3.

 
 
 
image
Neeraj  Amoli

Skills    Ruby On Rails

Qualifications :-
Location :-Dehradun,Dehradun,Uttrakhand,India
Description:-

I have 3 year experience as a Software Engineer. My Skilled are Android Development (Java), ROR Development .   


Explore
 

  Students (0)