Ruby On Rails Lesson
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
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.