[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed

Introduction

The implementation of the status feed that appears in the Rails tutorial was a bit complicated, so I'll summarize it myself.

Status feed

The status feed is a TL (tweet list) on Twitter. It is possible to display the posts of the users you are following.

Implementation method

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

References

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

[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
How to solve the local environment construction of Ruby on Rails (MAC)!
[Ruby On Rails] How to search the contents of params using include?
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Ruby on Rails] How to change the column name
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
How to use Ruby on Rails
[Ruby On Rails] How to search and save the data of the parent table from the child table
[Ruby on Rails] How to use CarrierWave
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
Things to remember and concepts in the Ruby on Rails tutorial
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
How to debug the processing in the Ruby on Rails model only on the console
How to find the cause of the Ruby error
[Ruby on Rails] How to display error messages
[Ruby on Rails] Until the introduction of RSpec
How to add / remove Ruby on Rails columns
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
How to resolve errors that occur in the "Ruby on Rails" integration test
How to implement image posting function using Active Storage in Ruby on Rails
[Ruby on Rails] Implement a pie chart that specifies the percentage of colors
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails] How to write enum in Japanese
Docker the development environment of Ruby on Rails project
[Rails, JS] How to implement asynchronous display of comments
[Ruby] How to find the sum of each digit
[Rails] How to change the column name of the table
[Ruby On Rails] How to reset DB in Heroku
[Rails] How to get the contents of strong parameters
Summary of how to implement default arguments in Java
(Ruby on Rails6) How to create models and tables
Try using the query attribute of Ruby on Rails
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Rails Tutorial Chapter 4] Rails-flavored Ruby
[Rails] How to implement scraping
[Ruby on Rails] How to implement tagging / incremental search function for posts (without gem)
Ruby on Rails validation summary
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
Ruby on Rails for beginners! !! Summary of new posting functions
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
(Ruby on Rails6) Display of the database that got the id of the database
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
How to display a graph in Ruby on Rails (LazyHighChart)
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
[Ruby] How to retrieve the contents of a double hash
Let's summarize how to extend the expiration date of Rails
[Rails] How to display the list of posts by category
How to run React and Rails on the same server
How to deploy a Rails application on AWS (article summary)
How to check the database of apps deployed on Heroku
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
[Ruby On Rails] How to update the calculated result to an integer type column using update_column
[Ruby on Rails] Since I finished all Rails Tutorial, I tried to implement an additional "stock function"
Ruby on Rails Overview (Beginner Summary)