我有一个推荐模型,我想选择所有与movie_id列截然不同的推荐。我尝试了一些方法,但没有一种方法可以使用:
scope :not_duplicate, -> {
group_by(&:movie_id)
}
==> doesn't work on a class
scope :not_duplicate, -> {
uniq_by(&:movie_id)
}
==> doesn't work on a class
scope :not_duplicate, -> {
select(:movie_id).group(:movie_id, "recommendations.id")
}
==> only returns movie_id values and I need entire record
scope :not_duplicate, -> {
select("recommendations.*").group(:movie_id)
}
==> PG::GroupingError: ERROR: column "recommendations.id" must appear in the GROUP BY clause or be used in an aggregate function
我确信这是一个非常常见的事情但由于某种原因我无法找到解决方案。有什么想法吗?
答案 0 :(得分:0)
如果我理解你的问题你可以试试这个
Recommendation.select {|rec| rec.movie_id == movie_id).uniq
答案 1 :(得分:0)
怎么样:
scope :not_duplicate, -> {
group(:id, :movie_id).distinct
}