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# select-fields
11 个解决方案
#1
364
Model.select(:rating)
Result of this is a collection of Model
objects. Not plain ratings. And from uniq
's point of view, they are completely different. You can use this:
结果是模型对象的集合。不是普通的评级。在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']
#2
80
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.
如果你要用模型。select,那么您也可以使用DISTINCT,因为它只返回惟一的值。这样做更好,因为这意味着它返回的行更少,并且应该比返回一些行并告诉Rails选择惟一的值快一些。
Model.select('DISTINCT rating')
Of course, this is provided your database understands the DISTINCT
keyword, and most should.
当然,如果您的数据库理解不同的关键字,并且大多数都应该这样做。
#3
49
This works too.
这个作品。
Model.pluck("DISTINCT rating")
#4
23
Model.uniq.pluck(:rating)
# SELECT DISTINCT "models"."rating" FROM "models"
This has the advantages of not using sql strings and not instantiating models
这具有不使用sql字符串和不实例化模型的优点
#5
20
Model.select(:rating).uniq
This code works as 'DISTINCT' (not as Array#uniq) since rails 3.2
这段代码的工作方式与rails 3.2不同(不是数组#uniq)
#6
14
If you want to also select extra fields:
如果你也想选择额外的字段:
Model.select('DISTINCT ON (models.ratings) models.ratings, models.id').map { |m| [m.id, m.ratings] }
#7
4
If anyone is looking for the same with Mongoid, that is
如果有人在找蒙古人,那就是
Model.distinct(:rating)
#8
4
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.
这些查询将被认为是“london”和“london”,这意味着它将忽略空格,这就是为什么它会在查询结果中一次选择“london”。
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查找数据。这些查询将认为“london”和“london”是不同的,这就是为什么在查询结果中会选择“london”和“london”。
please prefer to attached image for more understanding and have a look on "Toured / Awaiting RFP".
请选择附件图片了解更多,看一下“参观/等待RFP”。
#9
3
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)
因为,首先您要生成一个模型数组(由于选择而减小了大小),然后您要提取这些所选模型具有的惟一属性(评级)
#10
0
Model.select(:rating).distinct
#11
0
Another way to collect uniq columns with sql:
使用sql收集uniq列的另一种方法是:
Model.group(:rating).pluck(:rating)
#1
364
Model.select(:rating)
Result of this is a collection of Model
objects. Not plain ratings. And from uniq
's point of view, they are completely different. You can use this:
结果是模型对象的集合。不是普通的评级。在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']
#2
80
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.
如果你要用模型。select,那么您也可以使用DISTINCT,因为它只返回惟一的值。这样做更好,因为这意味着它返回的行更少,并且应该比返回一些行并告诉Rails选择惟一的值快一些。
Model.select('DISTINCT rating')
Of course, this is provided your database understands the DISTINCT
keyword, and most should.
当然,如果您的数据库理解不同的关键字,并且大多数都应该这样做。
#3
49
This works too.
这个作品。
Model.pluck("DISTINCT rating")
#4
23
Model.uniq.pluck(:rating)
# SELECT DISTINCT "models"."rating" FROM "models"
This has the advantages of not using sql strings and not instantiating models
这具有不使用sql字符串和不实例化模型的优点
#5
20
Model.select(:rating).uniq
This code works as 'DISTINCT' (not as Array#uniq) since rails 3.2
这段代码的工作方式与rails 3.2不同(不是数组#uniq)
#6
14
If you want to also select extra fields:
如果你也想选择额外的字段:
Model.select('DISTINCT ON (models.ratings) models.ratings, models.id').map { |m| [m.id, m.ratings] }
#7
4
If anyone is looking for the same with Mongoid, that is
如果有人在找蒙古人,那就是
Model.distinct(:rating)
#8
4
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.
这些查询将被认为是“london”和“london”,这意味着它将忽略空格,这就是为什么它会在查询结果中一次选择“london”。
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查找数据。这些查询将认为“london”和“london”是不同的,这就是为什么在查询结果中会选择“london”和“london”。
please prefer to attached image for more understanding and have a look on "Toured / Awaiting RFP".
请选择附件图片了解更多,看一下“参观/等待RFP”。
#9
3
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)
因为,首先您要生成一个模型数组(由于选择而减小了大小),然后您要提取这些所选模型具有的惟一属性(评级)
#10
0
Model.select(:rating).distinct
#11
0
Another way to collect uniq columns with sql:
使用sql收集uniq列的另一种方法是:
Model.group(:rating).pluck(:rating)