Rails:如何从列获取惟一值

时间:2022-11-24 21:45:15

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

如何从表中获得唯一的值?例如,我有这个产品表:

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:

这里我只想得到两个值——1st_cat和2nd_cat:

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

6 个解决方案

#1


123  

Two more ways:

两个方面:

Products.select(:category).map(&:category).uniq

Products.uniq.pluck(:category)

#2


25  

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

#3


9  

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将生成适用于任何数据库的SQL (Postgres、MySQL等)。

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

#4


6  

Try this (in the rails console)

试试这个(在rails控制台)

Product.group(:category)

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

#5


4  

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

我建议使用product .all. distinc(:类别),因为uniq自rails 5以来就被弃用,并且将在rails 5.1上移除

#6


0  

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_user method 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 %>

#1


123  

Two more ways:

两个方面:

Products.select(:category).map(&:category).uniq

Products.uniq.pluck(:category)

#2


25  

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

#3


9  

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将生成适用于任何数据库的SQL (Postgres、MySQL等)。

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

#4


6  

Try this (in the rails console)

试试这个(在rails控制台)

Product.group(:category)

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

#5


4  

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

我建议使用product .all. distinc(:类别),因为uniq自rails 5以来就被弃用,并且将在rails 5.1上移除

#6


0  

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_user method 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 %>