How to allow users to search only their posts usin

2019-08-10 03:50发布

I want users to be able to search only their posts and not show the posts created by other users. How exactly would I do that? I'm using Elastic Search with Searchkick gem.

I tried this but it is still showing posts from other users:

posts controller

@posts = current_user.posts.search params[:search]

2条回答
够拽才男人
2楼-- · 2019-08-10 04:11

According to searchkick, the following should work:

Post.search params[:search], where: {user_id: current_user.id}
查看更多
甜甜的少女心
3楼-- · 2019-08-10 04:26

Maybe you need to add a where clause to only search the post of the user

@posts = current_user.posts.where(user_id: current_user.id).search(params[:search])
查看更多
登录 后发表回答