The implementation of the status feed
that appears in the Rails tutorial was a bit complicated, so I'll summarize it myself.
The status feed is a TL (tweet list) on Twitter. It is possible to display the posts of the users you are following.
Create a feed
method.
user.rb
#Returns a feed of status.
def feed
end
First, I will draw from the conclusion. Describe as follows in the feed
method.
user.rb
#Returns a feed of status.
def feed
following_ids = "SELECT followed_id FROM relationships WHERE follower_id = :user_id"
Micropost.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id)
end
Even if you look at this alone, it's Wakewakarimasen. Let's take a closer look. First, focus on the following.
user.rb
following_ids = "SELECT followed_id FROM relationships WHERE follower_id = :user_id"
The above code is represented by a SQL statement and the SELECT
command is used.
SQL command | meaning |
---|---|
SELECT | Search the data in the table. |
SELECT command parameters | meaning |
---|---|
FROM | Specify the target source table. |
WHERE | Set the condition of the value you want to get |
In other words, what we mean here is ...
This means that the followed_id
column of the relationships
table gets the users that match the user_id
. You can get the following_ids
variable ** following user information **.
Next, let's focus on the code below.
user.rb
Micropost.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id)
It uses the rails where
method.
The where
method was also a little complicated to use, so I will organize it.
First, let's look at IN
and OR
.
IN
is used to define ** multiple conditions **.
An example is given below.
#Single designation
#Gets the user whose age column is "20".
user = User.where("age = 20")
#Multiple designation
#Get users whose age column is "20 and 30".
user = User.where("age IN (20, 30)")
If you go back to the code above and think about it ...
The value of user_id
will get the post with the id value of following_ids
(list of following users) defined earlier.
user.rb
Micropost.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id)
Next, let's focus on OR
.
OR
means ** to get data that matches either condition **.
An example is given below.
#Gets the user whose name column is "Taro" and whose age column is "20".
user = User.where("name = 'Taro' and age = 20")
If you go back to the code above and think about it ...
If the value of user_id
is the id of following_ids
(list of following users) or "id" (your id), that value will be returned.
user.rb
Micropost.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id)
By the way, for the part of user_id =: user_id", user_id: id
, the value specified by :
is assigned to the value specified after ,
.
python
#Gets the user whose age column is "20".
user = User.where("age = :xxx", xxx: 20)
#Gets the user whose age column is "20".
user = User.where("age = 20")
I understand the feed method. You can use the feed method as follows to get posts from users that the logged-in user is following.
current_user.feed
Rails tutorial Chapter 14 Follow users https://.jp/chapters/following_users?version=6.0#sec-the_status_feed
Pikawaka [Rails] Let's get the desired data using the where method! https://pikawaka.com/rails/where
Recommended Posts