用户快照-如何仅显示您关注的用户的快照(我自己的用户除外)

时间:2020-06-30 13:50:17

标签: ruby-on-rails feed show

如何仅显示您关注的用户的照片(我自己的用户除外)? 我认为问题出在Feed中,但是我在Rails中是新手,也不知道如何做。感谢开发人员!

shot from show page

[enter image description here][1]

my user.controller

    class User < ApplicationRecord
friendships, class_name: "Friendship", foreign_key: "follower_id", dependent: :destroy
      has_many :passive_friendships, class_name: "Friendship", foreign_key: "followed_id", dependent: :destroy

      has_many :followings, through: :active_friendships, source: :followed
      has_many :followers, through: :passive_friendships, source: :follower

      #follow another user
      def follow(other_user)
        active_friendships.create(followed_id: other_user.id)
      end

      #Unfollow a other_user
      def unfollow(other_user)
        active_friendships.find_by(followed_id: other_user.id).destroy
      end
      #Is following a other_user?
      def following?(other_user)
        following_ids.include?(other_user.id)
      end

      def feed
        following_ids = "SELECT followed_id FROM Friendships WHERE follower_id = :user_id"
        Shot.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id)
      end

my show.html.erb

      <div class="shots user">
        <% @user.feed.each do|shot|%>
        <section class="section">
          <div class="shot-grid-item">
            <div class="shot-wrapper">
              <%= link_to shot, class: "shot" do %>
              <% if shot.user_shot_url.present? %>
              <%= cl_image_tag(shot.user_shot_url) %>
              <% else %>
              <label><%= shot.description %></label>
              <div class="shot-data">
                <h3 class="shot-title"><%= shot.title %></h3>
                <div class="shot-description"><%= truncate(shot.description, length: 60) %></div>
                <div class="shot-time">
                  <%= time_ago_in_words(shot.created_at) %>
                </div>
              </div>
              <% end %>
              <% end%>
       </div>

1 个答案:

答案 0 :(得分:0)

您可以做类似的事情

<%= current_user.followings.shots.each do |shot| %>
<% end %>

更好-您可以在控制器中声明一个变量,该变量将从视图中提取此逻辑:

@following_shots = current_user.followings.shots

然后在视图中

<%= @following_shots.each do |shot| %>
<% end %>