Home Rails Include Current User's Post in Timeline
Reply: 2

Rails Include Current User's Post in Timeline

chronycles Published in 2018-02-14 07:27:12Z

I have rails project where user have timeline which include posts of users which he/she follows. I made a code in Post model:

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

So I can show the Posts in PostController like this:

@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. How am I able to do this?

Thank you very much!

Vishal Reply to 2018-02-14 11:53:19Z

Try below query

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)
Elayan Hamamrah
Elayan Hamamrah Reply to 2018-02-14 08:18:59Z

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)

It happens when you are in 'create' action for the post, ask the model to handle the following concept.


def create
 if @post.save 
  bla bla bla

Post Model:

after_save :follow_post

def follow_post
 do the follow action 
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.323074 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO