1

我需要帮助改进这个查询

def interesting_books
  Book.joins(:genre)
    .where(author: interesting_authors.sample(5))
    .where('ratings >= 4')
    .random_order
    .where.not(id: black_books)
    .limit(3)
end

def interesting_authors
 @interesting_authors ||= (authors_commented_books + authors_watched_books).uniq
end

def authors_commented_books
 @authors_commented_books ||= current_user.commented_books.pluck(:author).uniq
end

def authors_watched_books
 @authors_watched_books ||= current_user.watched_books.pluck(:author).uniq
end

现在,以这种方式,如果.where(author: interesting_authors.sample(5))我有像“莎士比亚”,“吐温”,“菲茨杰拉德”,“王尔德”和“达尔文”这样的作者......它只显示由“莎士比亚”制作的3本书(.limit(3)) ”。结果:《麦克白》、《奥赛罗》和《暴风雨》。

但我想展示由不同(选定)作者制作的 3 本书。像《奥赛罗》、《了不起的盖茨比》和《道林格雷》。

怎么做?

4

0 回答 0