How to display shots from only users you follow (excluding my own)? I think the problem is in the feed, but I'm very new in Rails, and have no idea how to do it. thanks Devs!
[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>