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:- 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
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)