更新时间:2022-10-18 11:38:01
Model.select(:评分)
这个结果是型号
对象的数组。不是普通的收视率。而从 uniq的
的角度来看,他们是完全不同。您可以使用此:
Model.select(:等级).MAP(安培;:评级).uniq
或此(最有效的)
Model.uniq.pluck(:评分)
I already have a working solution, but I would really like to know why this doesn't work:
ratings = Model.select(:rating).uniq
ratings.each { |r| puts r.rating }
It selects, but don't print unique values, it prints all values, including the duplicates. And it's in the documentation: http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields
Model.select(:rating)
Result of this is an array of Model
objects. Not plain ratings. And from uniq
's point of view, they are completely different. You can use this:
Model.select(:rating).map(&:rating).uniq
or this (most efficient)
Model.uniq.pluck(:rating)