目次 閉じる
フォローした人の記事をタイムラインに表示する
前回まででフォローの相互関係づくりが完了した
今回は、フォローした人の記事をタイムラインに表示していく
routes.rbを開く
resource :timeline, only: [:show]timelineは1人ひとつのため、単数形とする
timelines_controller.rbを作る
class TimelinesController < ApplicationController
before_action :authenticate_user!
def show
end
end def show
current_user.followings.pluck(:id)pluck(:id)
followしているuserを取得し、user1, user2 => [2, 3] が返ってくる

Articleのuser_idが、pluck(:id)で取得したuser_idと合致する物を探す
def show
user_ids = current_user.followings.pluck(:id)
@articles = Article.where(user_id: user_ids)
end配列(user_ids)を渡すと、whereは「or」検索する
- user_id = “1” の記事と user_id = “2” の記事を探してくる
- articlesに配列が入る
#DAY4

