简体   繁体   English

Rails在时间轴中包括当前用户的帖子

[英]Rails Include Current User's Post in Timeline

I have rails project where user have timeline which include posts of users which he/she follows. 我有Rails项目,其中用户具有时间表,其中包括他/她关注的用户帖子。 I made a code in Post model: 我在Post模型中编写了代码:

scope :of_followed_users, -> (following_users) { where user_id: following_users }

So I can show the Posts in PostController like this: 所以我可以这样显示PostController中的帖子:

@posts = Post.where(user_id: @current_user.following)order('created_at DESC').paginate(page: params[:page], per_page: 20)

But the problem is that I want users also could see his/her posts in timeline nevertheless. 但是问题是我仍然希望用户也可以在时间轴上看到他/她的帖子。 What is the best practice to do that? 最佳做法是什么? Do I have to follow myself first? 我必须先跟随自己吗? I have tried using an array where user_id: [@current_user.following, @current_user] but it just shows the current_user's post. 我尝试使用其中user_id: [@current_user.following, @current_user]的数组user_id: [@current_user.following, @current_user]但它仅显示current_user的帖子。 How am I able to do this? 我该怎么做?

Thank you very much! 非常感谢你!

尝试以下查询

Post.where("user_id IN (?) OR user_id = ? ", @current_user.following.ids,@curretnt_user.id).order('created_at DESC').paginate(page: params[:page], per_page: 20)

In another concept: Why you don't add an action to the posts which will make the owner of the post as a follower as well to the post. 在另一个概念中:为什么不对帖子添加操作,这会使帖子的所有者也成为帖子的关注者。

Example: User(x) Added a Post(Y) User(x) is a follower of Post(Y) 示例:用户(x)添加了帖子(Y)用户(x)是帖子(Y)的关注者

It happens when you are in 'create' action for the post, ask the model to handle the following concept. 当您在帖子的“创建”操作中发生这种情况时,请模型处理以下概念。

PostController: PostController:

def create
 if @post.save 
  bla bla bla
 end
end 

Post Model: 帖子模型:

after_save :follow_post

private 
def follow_post
 do the follow action 
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM