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:- Micropost Refinements

The key line here is

@user.microposts.should == [newer micropost, older micropost]

 

indicating that the posts should be ordered newest first. This should fail because by default the posts will be ordered by id, i.e., [older_micropost, newer_micropost]. This test also verifies the basic correctness of the has_many association itself, by checking (as indicated in Table 10.1) that user.microposts is an array of microposts. To get the ordering test to pass, we use a Rails facility called default_scope with an :order parameter, as shown in Listing 10.14. (This is our first example of the notion of scope. We will learn about scope in a more general context in Chapter 11.)  

 

The order hereis ’microposts.created_at DESC’, where DESC is SQL for ‘‘descending,’’ i.e., in descending order from newest to oldest.

Dependent: Destroy

Apart from proper ordering, there is a second refinement we’d like to add to microposts. Recall from Section 9.4 that site administrators have the power to destroy users. It stands to reason that, if a user is destroyed, the user’s microposts should be destroyed as well. We can test for this by first destroying a micropost’s user and then verifying that the associated microposts are no longer in the database (Listing 10.15).

 

 
 
 
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)