act_as_followers获取我关注的所有用户的帖子
Im试图从Users
获取所有Posts
。我正在使用act_as_follower宝石。用户遵循profile
模型,并且Posts
属于User
。act_as_followers获取我关注的所有用户的帖子
我的用户模型:
acts_as_follower
我的剖面模型的用户如下:
belongs_to :user
acts_as_followable
Post模型:
belongs_to :user
我的帖子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")
但事实是用户遵循剖面模型,但在这里,我正在寻找一种“邮报”。
编辑
在我的索引控制器心中已经建立如下:
@favoritePost = Post.where(user_id: current_user.all_follows.pluck(:id))
并在视图IV实现这一点:
<% if user_signed_in? %>
<%@favoritePost.each do |post| %>
<%= post.title %>
<% end %>
<% 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 %>
宝石让你跟着多个模型和follows_by_type('Post')
筛选您关注的帖子。
您要做的是从您关注的用户处返回帖子。
控制器
@posts = Post.where(user_id: current_user.all_following.pluck(:id))
查看
<% @posts.each do |post| %>
<%= post.title %>
<% end %>
我会需要添加'acts_as_followable'我的帖子模式? –
@GurmukhSingh你不需要,因为你没有关注帖子,只是用户。 –
我得到未定义的方法'all_follows'为零:NilClass –
我已经更新了我对你的回答。 –