Ruby-on-rails Rails:从列中选择唯一值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9658881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: select unique values from a column
提问by alexandrecosta
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
它选择但不打印唯一值,它打印所有值,包括重复值。它在文档中:http: //guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields
回答by Sergio Tulentsev
Model.select(:rating)
Result of this is a collection of Modelobjects. Not plain ratings. And from uniq's point of view, they are completely different. You can use this:
这样做的结果是Model对象的集合。不是简单的评分。而从uniq的角度来看,它们是完全不同的。你可以使用这个:
Model.select(:rating).map(&:rating).uniq
or this (most efficient)
或者这个(最有效)
Model.uniq.pluck(:rating)
# rails 5+
Model.distinct.pluck(:rating)
Update
更新
Apparently, as of rails 5.0.0.1, it works only on "top level" queries, like above. Doesn't work on collection proxies ("has_many" relations, for example).
显然,从 rails 5.0.0.1 开始,它只适用于“顶级”查询,就像上面一样。不适用于集合代理(例如,“has_many”关系)。
Address.distinct.pluck(:city) # => ['Moscow']
user.addresses.distinct.pluck(:city) # => ['Moscow', 'Moscow', 'Moscow']
In this case, deduplicate after the query
在这种情况下,在查询后进行重复数据删除
user.addresses.pluck(:city).uniq # => ['Moscow']
回答by kakubei
If you're going to use Model.select, then you might as well just use DISTINCT, as it will return only the unique values. This is better because it means it returns less rows and should be slightly faster than returning a number of rows and then telling Rails to pick the unique values.
如果您要使用Model.select,那么您不妨直接使用DISTINCT,因为它只会返回唯一值。这更好,因为这意味着它返回更少的行并且应该比返回一些行然后告诉 Rails 选择唯一值稍微快一点。
Model.select('DISTINCT rating')
Of course, this is provided your database understands the DISTINCTkeyword, and most should.
当然,前提是您的数据库理解DISTINCT关键字,并且大多数应该理解。
回答by Nat
This works too.
这也有效。
Model.pluck("DISTINCT rating")
回答by Marcin Nowicki
If you want to also select extra fields:
如果您还想选择额外的字段:
Model.select('DISTINCT ON (models.ratings) models.ratings, models.id').map { |m| [m.id, m.ratings] }
回答by Cameron Martin
Model.uniq.pluck(:rating)
# SELECT DISTINCT "models"."rating" FROM "models"
This has the advantages of not using sql strings and not instantiating models
这具有不使用sql字符串和不实例化模型的优点
回答by kuboon
回答by uma
If I am going right to way then :
如果我要走正确的路,那么:
Current query
当前查询
Model.select(:rating)
is returning array of object and you have written query
正在返回对象数组并且您已经编写了查询
Model.select(:rating).uniq
uniq is applied on array of object and each object have unique id. uniq is performing its job correctly because each object in array is uniq.
uniq 应用于对象数组,每个对象都有唯一的 id。uniq 正在正确执行其工作,因为数组中的每个对象都是 uniq。
There are many way to select distinct rating :
有很多方法可以选择不同的评分:
Model.select('distinct rating').map(&:rating)
or
或者
Model.select('distinct rating').collect(&:rating)
or
或者
Model.select(:rating).map(&:rating).uniq
or
或者
Model.select(:name).collect(&:rating).uniq
One more thing, first and second query : find distinct data by SQL query.
还有一件事,第一次和第二次查询:通过 SQL 查询找到不同的数据。
These queries will considered "london" and "london " same means it will neglect to space, that's why it will select 'london' one time in your query result.
这些查询将被视为“伦敦”和“伦敦”,这意味着它会忽略空间,这就是为什么它会在您的查询结果中一次选择“伦敦”。
Third and forth query:
第三次和第四次查询:
find data by SQL query and for distinct data applied ruby uniq mehtod. these queries will considered "london" and "london " different, that's why it will select 'london' and 'london ' both in your query result.
通过 SQL 查询查找数据并应用 ruby uniq mehtod 来获取不同的数据。这些查询将被视为“伦敦”和“伦敦”不同,这就是为什么它会在您的查询结果中选择“伦敦”和“伦敦”。
please prefer to attached image for more understanding and have a look on "Toured / Awaiting RFP".
请更喜欢附加图片以获得更多理解,并查看“Toured / Awaiting RFP”。


回答by hassan_i
Model.select(:rating).distinct
回答by Fernando Fabreti
Some answers don't take into account the OP wants a array of values
一些答案没有考虑到 OP 想要一组值
Other answers don't work well if your Model has thousands of records
如果您的模型有数千条记录,则其他答案效果不佳
That said, I think a good answer is:
也就是说,我认为一个很好的答案是:
Model.uniq.select(:ratings).map(&:ratings)
=> "SELECT DISTINCT ratings FROM `models` "
Because, first you generate a array of Model (with diminished size because of the select), then you extract the only attribute those selected models have (ratings)
因为,首先您生成一个模型数组(由于选择而减小了大小),然后您提取了那些选定模型具有的唯一属性(评级)
回答by Vassilis
If anyone is looking for the same with Mongoid, that is
如果有人在寻找与 Mongoid 相同的东西,那就是
Model.distinct(:rating)

