2016-12-02 43 views
2

我必須搜索書籍,用戶通過作者的書籍需要能夠搜索他們的書籍而不顯示其他用戶的書籍。使用Elasticsearch和Searchkick on Rails通過關聯查找用戶項目?

書籍模型

has_many :authors 
has_many :users, through: :authors 

我試圖描述here進行搜索,但沒有奏效。

@Books = Book.search "*", where: { user_id: current_user.id} 

我認爲它沒有工作,因爲books模型沒有user_id

回答

1

有可能是你能做到這一點的幾種方式。 這裏有一個:搜索通過searchkick的圖書索引,但只返回其ID匹配的控制器的用戶book_ids

用戶模型

# assuming you have: 
has_many :books, through: :authors 

一書

# change your where clause to filter based on book id 
@Books = Book.search "*", where: { id: current_user.book_ids} 
相關問題