Ruby On Rails Classroom image

Neeraj  Amoli / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  |1 | 2 | 3 | 4|Last
Lessons:- A First Feed Implementation

  A First Feed Implementation

Now it’s time to implement Micropost.from_users_followed_by, which for simplicity we’ll just refer to as ‘‘the feed.’’ Since the final result is rather intricate, we’ll build up to the final feed implementation by introducing one piece at a time. The first step is to think of the kind of query we’ll need. What we want to do is select from the microposts table all the microposts with ids corresponding to the users being followed by a given user (or the user itself). We might write this schematically as follows:

 

In writing this code, we’ve guessed that SQL supports an IN keyword that allows us to test for set inclusion. (Happily, it does.)

         Recall from the proto-feed in Section 10.3.3 that Active Record uses the where method to accomplish the kind of select shown above, as illustrated in Listing 10.39. There, our select was very simple; we just picked out all the microposts with user id corresponding to the current user:

Micropost.where  (  "user id = ?", id )

 

Here, we expect it to be more complicated, something like

where  ( "user id in (?) OR user id = ?", following ids, user)  

 

(Here we’ve used the Rails convention of user instead of user.id in the condition; Rails automatically uses the id. We’ve also omitted the leading Micropost. since we expect this method to live in the Micropost model itself.)

        We see from these conditions that we’ll need an array of ids corresponding to the users being followed. One way to do this is to use Ruby’s map method, available on any ‘‘enumerable’’ object, i.e., any object (such as an Array or a Hash) that consists of a collection of elements.11 We saw an example of this method in Section 4.3.2; it works like this:

 
 
 
image
Neeraj  Amoli

Skills    Ruby On Rails

Qualifications :-
Location :-Dehradun,Dehradun,Uttrakhand,India
Description:-

I have 3 year experience as a Software Engineer. My Skilled are Android Development (Java), ROR Development .   


Explore
 

  Students (0)