act_as_followers 获取我关注的用户的所有帖子

act_as_followers get all posts of user I'm following

我正在尝试从 Users 获取所有 Posts。我正在使用 act_as_follower gem。用户遵循 profile 模型,Posts 属于 User

我的用户模型:

  acts_as_follower

用户关注我的个人资料模型:

  belongs_to :user
  acts_as_followable

Post 型号:

  belongs_to :user

我的 Post Controller.rb:

  def follow
    @profile = Profile.find(params[:id])
    current_user.follow(@profile)
    redirect_to :back
  end

  def unfollow
    @profile = Profile.find(params[:id])
    current_user.stop_following(@profile)
    redirect_to :back
  end

我正在尝试使用提供的 follows_by_type 方法来实现类似的功能:

@posts = current_user.follows_by_type('Post').order("created_at DESC")

但问题是 User 遵循 Profile 模型,但我在这里寻找类型 'Post'。

编辑

在我的索引控制器中,我设置了以下内容:

@favoritePost = Post.where(user_id: current_user.all_follows.pluck(:id))

并且在视图 iv 中实现了这个:

<% if user_signed_in? %>
   <%@favoritePost.each do |post| %>
      <%= post.title %>
   <% end %>
<% end %>

gem 允许您关注多个模特并 follows_by_type('Post') 过滤您关注的帖子。

您要做的是 return 您关注的用户的帖子。

控制器

@posts = Post.where(user_id: current_user.all_following.pluck(:id))

查看

<% @posts.each do |post| %>
  <%= post.title %>
<% end %>

我制定了这个解决方案,可能不是最好的,但它按要求工作。

在我的控制器中我设置了这个:

@following = current_user.all_following
@followposts = @following.each do |f|
  f.user.id
end

在我看来我已经设置好了:

<% @followposts.each do |f| %>
  <% f.user.posts.each do |g| %>
    <%= g.title %>
  <% end %>
<% end %>