我必须搜索书籍,用户通过作者拥有书籍需要能够只搜索他们的书籍而不显示其他用户的书籍。
书籍模型
has_many :authors
has_many :users, through: :authors
我尝试按照描述进行搜索 here但它没有用。
@Books = Book.search "*", where: { user_id: current_user.id}
我认为它不起作用,因为 books
模型没有 user_id
最佳答案
您可能有几种方法可以做到这一点。 这是一个:在 searchkick 中搜索图书索引,但只返回 ID 与其中一个用户 book_id 匹配的图书
用户模型
# assuming you have:
has_many :books, through: :authors
在 Controller 中
# change your where clause to filter based on book id
@Books = Book.search "*", where: { id: current_user.book_ids}
关于ruby-on-rails - 使用 Elasticsearch 与 Searchkick on Rails 通过关联查找用户项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40937275/