我已经有一个有效的解决方案,但我真的想知道为什么这不起作用:
收视率= Model.select(:评级).uniq
ratings.each {| R |把r.rating}
它可以选择,但是不打印唯一值,它打印的所有值,包括重复。而且它在文档中:http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields
解决方案 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)