Ruby-on-rails Rails:如何从列中获取唯一值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8369812/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:26:17  来源:igfitidea点击:

Rails: how can I get unique values from column

ruby-on-railsruby-on-rails-3unique

提问by Oleg Pasko

How can I get unique values from column in the table? For example, I have this Products table:

如何从表中的列中获取唯一值?例如,我有这个 Products 表:

ID NAME CATEGORY
1 name1 1st_cat
2 name2 2nd_cat
3 name3 1st_cat

Here I want to get only 2 values - 1st_cat and 2nd_cat:

在这里,我只想获得 2 个值 - 1st_cat 和 2nd_cat:

<%Products.each do |p|%>
<%=p.category%>
<%end%>

回答by user664833

Two more ways:

还有两种方式:

Product.select(:category).map(&:category).uniq # Ruby does the work

Product.uniq.pluck(:category) # DB does the work (superior)

For Rails >= 5.1use:

对于Rails >= 5.1使用:

Product.distinct.pluck(:category) # DB does the work (superior)

...because Relation#uniqwas deprecated.

...因为Relation#uniq已被弃用

回答by ThatOtherPerson

I think you can do this:

我认为你可以这样做:

<% Products.select("DISTINCT(CATEGORY)").each do |p| %>
<%= p.category %>
<% end %>

Source: http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields

来源:http: //guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields

回答by jelder

This does all the work in the database server. The result is a simple array.

这完成了数据库服务器中的所有工作。结果是一个简单的数组。

<% Product.distinct(:category).pluck(:category).each do |category|
    <%= category %>
<% end %>

Rails will generate SQL that works on any database (Postgres, MySQL, etc).

Rails 将生成适用于任何数据库(Postgres、MySQL 等)的 SQL。

SELECT DISTINCT "products"."category" FROM "products"

回答by Fabio Ros

I suggest to use Products.all.distinct.pluck(:category)because uniqhas been deprecated since rails 5 and it will be removed on rails 5.1

我建议使用Products.all.distinct.pluck(:category)因为uniq自 rails 5 以来已被弃用,它将在 rails 5.1 上删除

回答by Marek P?íhoda

Try this (in the rails console)

试试这个(在 rails 控制台中)

Product.group(:category)

Product.group(:category).each { |p| p.name }

回答by srghma

For postgres

对于 postgres

<% Product.select("DISTINCT ON (category) *").each do |category|
    <%= category %>
    <%= name %>
<% end %>


Update

更新

even better

甚至更好

<% Product.select(%(DISTINCT ON (category) "#{Product.table_name}".*)).each do |category|
    <%= category %>
    <%= name %>
<% end %>

because it can return wrong columns when you do joins(e.g. returns idcolumn from joined table, but not products)

因为它可以在你这样做时返回错误的列joins(例如id从连接表中返回列,但不是products

回答by Phil_ish

Needed to get unique output and was trying the 'uniq' method unsuccessfully. Tried several solutions posted here unsuccessfully. I'm using devise which gives me access to the current_usermethod and working with two tables, one being a join (an item has_many :things).

需要获得唯一的输出并且尝试使用“uniq”方法失败。尝试了此处发布的几种解决方案均未成功。我正在使用设计,它使我可以访问该current_user方法并使用两个表,一个是连接(一个项目 has_many :things)。

This solution ultimately worked for me :

这个解决方案最终对我有用:

@current_user.things.select(:item_fk).distinct.each do |thing|
 <%= thing.item.attribute %>
<% end %>