Ruby On Rails Classroom image

prateek  darmwal / 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
prateek  darmwal

Skills    Ruby On Rails

Qualifications :- High School - S.K.M. Sn. Sec. School, Haldwani, College/University - Graphic Era Hill University, Bhimtal,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:- I like to explore new technologies. I have skills in ruby on rails, php5, cakephp, jquery, javascript, html/css, java, c & c++. I love coding
Explore
 

  Students (0)