yuyanegishi / issue_check

0 stars 0 forks source link

★Rails Turtorial:ステータスフィード #6

Open yuyanegishi opened 5 years ago

yuyanegishi commented 5 years ago
def home
    if logged_in?
      @micropost  = current_user.microposts.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
end
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
<% if logged_in? %>
  <div class="row">
    <aside class="col-md-4">
      <section class="user_info">
        <%= render 'shared/user_info' %>
      </section>
      <section class="stats">
        <%= render 'shared/stats' %>
      </section>
      <section class="micropost_form">
        <%= render 'shared/micropost_form' %>
      </section>
    </aside>
    <div class="col-md-8">
      <h3>Micropost Feed</h3>
      <%= render 'shared/feed' %>
    </div>
  </div>
<% else %>
  <div class="center jumbotron">
    <h1>Welcome to the Sample App </h1>

    <h2>
      This is the home page for the
      <a href="https://railstutorial.jp/">Ruby on Rails Tutorial</a>
      sample application.
    </h2>

    <%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
  </div>

  <%= link_to image_tag("rails.png", alt: "Rails logo"), 'http://rubyonrails.org/' %>
<% end %>
<% if @feed_items.any? %>
  <ol class="microposts">
    <%= render @feed_items %>
  </ol>
  <%= will_paginate @feed_items %>
<% end %>

内容

following_ids = "SELECT followed_id FROM relationships WHERE follower_id = :user_id"

リファレンスの確認 ※スッキリわかるSQL入門

内容

Micropost.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id)

リファレンスの確認 ※スッキリわかるSQL入門、Ruby on Rails API

where(opts = :chain, *rest)

Returns a new relation, which is the result of filtering the current relation according to the conditions in the arguments.

where accepts conditions in one of several formats. In the examples below,
the resulting SQL is given as an illustration; the actual query generated may be different depending on the database adapter.
yuyanegishi commented 5 years ago
Micropost.where("user_id = 1")   

=>#<ActiveRecord::Relation [#<Micropost id: 303, content: "テストテスト", user_id: 1, created_at: "2018-11-29 06:08:08", updated_at: "2018-11-29 06:08:08", picture: nil>, #<Micropost id: 295, content: "Ipsa voluptatem modi et a omnis.", user_id: 1, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29", picture: nil>, #<Micropost id: 289, content: "Commodi aut quam optio ipsum in aut voluptas reici...", user_id: 1, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29", picture: nil>, #<Micropost id: 283, content: "Ducimus eos repudiandae labore quia dolorem sit.", user_id: 1, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29", picture: nil>, #<Micropost id: 277, content: "Nostrum accusamus ullam ut ducimus praesentium non...", user_id: 1, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29", picture: nil>, #<Micropost id: 271, content: "Et et veritatis repudiandae voluptatibus unde labo...", user_id: 1, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29", picture: nil>, #<Micropost id: 265, content: "Eos doloribus quisquam itaque quam sed autem accus...", user_id: 1, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29", picture: nil>, #<Micropost id: 259, content: "Veniam aspernatur voluptate deserunt in repellendu...", user_id: 1, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29", picture: nil>, #<Micropost id: 253, content: "Nisi ipsam optio officia quidem tenetur in corpori...", user_id: 1, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29", picture: nil>, #<Micropost id: 247, content: "Fugiat vel deleniti quos corporis quo consectetur ...", user_id: 1, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29", picture: nil>, ...]>