Ruby On Rails Classroom image

Anil  Bist / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4|Last
Lessons:-Augmenting the User Show Page

Note here that we can use the count method through the association:

user.microposts.count

 

The association count method is smart and performs the count directly in the database. In particular, it does not pull all the microposts out of the database and then call length on the resulting array, as this could become inefficient as the number of microposts grew. Instead, it asks the database to count the microposts with the given user_id. In the unlikely event that finding the count is still a bottleneck in your application, you can make it even faster with a counter cache.

    Although the tests in Listing 10.19 won’t pass until Listing 10.21, we’ll get started on the application code by inserting a list of microposts into the user profile page, as shown in Listing 10.20.  

 

 

We will deal with the microposts list momentarily, but there are several other things to note first. In Listing 10.20, the use of if @user.microposts.any? (a construction we saw before in Listing 7.23) makes sure that an empty list won’t be displayed when the user has no microposts.

       Also note from Listing 10.20 that we’ve preemptively added pagination for microposts through

<%=  will paginate @microposts  %>

 

If you compare this with the analogous line on the user index page, Listing 9.34, you’ll see that before we had just

<%=  will paginate  %>  
 
 
 
image
Anil  Bist

Skills    Ruby On Rails

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)