Ruby On Rails Classroom image

Anil  Bist / 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
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)