Rails show posts from only users that the current user is following
I'm new to Rails a开发者_开发知识库nd having a go at a twitter type web app.
The 'following' and 'followers' part of the app is fine. However I want to filter the Posts index to only show posts that the current user is following.
I need to do something along the lines of:
@posts = Post.where('user_id LIKE ?', current_user.friendships )
As I expected this throws up an error
SQLite3::SQLException: near ",": syntax error: SELECT "posts".* FROM "posts" WHERE (user_id LIKE 8,9)
It seems close to what I want because 8,9 are the id's of the friends I am following.
Any suggestions as to the best way to get what I'm looking for?
Thanks!
Try IN
instead of LIKE
:
@posts = Post.where("user_id IN (?)", current_user.friendships)
Or:
@posts = Post.where(:user_id => current_user.friendships)
精彩评论