Ruby-on-rails 如何获得此数组中的唯一元素?

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

How do I get unique elements in this array?

ruby-on-railsruby

提问by Christian Fazzini

Using Mongoid. Unfortunately, Mongoid does not allow for selecting unique / distinct! Have gotten these results. As you can see, there are 7 results. If you look carefully (at user_id), there are only 2 users.

使用蒙古包。不幸的是,Mongoid 不允许选择独特的/独特的!得到了这些结果。如您所见,有 7 个结果。如果仔细看(在 user_id 处),只有 2 个用户。

[
  #<Activity _id: 4cea6c4572357e00fa00011a, created_at: 2010-11-22 13:12:37 UTC, updated_at: 2010-11-22 13:12:37 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea6c3072357e00fa000116, created_at: 2010-11-22 13:12:16 UTC, updated_at: 2010-11-22 13:12:16 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea6bdd72357e00fa00010d, created_at: 2010-11-22 13:10:53 UTC, updated_at: 2010-11-22 13:10:53 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea46df72357e00fa0000a4, created_at: 2010-11-22 10:33:03 UTC, updated_at: 2010-11-22 10:33:03 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea40c572357e00fa00006f, created_at: 2010-11-22 10:07:01 UTC, updated_at: 2010-11-22 10:07:01 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea3c8b72357e00fa00005e')>, 
  #<Activity _id: 4cea3ca172357e00fa000062, created_at: 2010-11-22 09:49:21 UTC, updated_at: 2010-11-22 09:49:21 UTC, action: "Attend", user_id: BSON::ObjectId('4cea39b772357e00fa000046'), artist_id: nil, media_id: BSON::ObjectId('4cea3c8b72357e00fa00005e')>, 
  #<Activity _id: 4cea344a72357e00fa00003f, created_at: 2010-11-22 09:13:46 UTC, updated_at: 2010-11-22 09:13:46 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea306c72357e00fa000031')>
] 

I was looking at this, and was thinking I could do something similar so that my array would now look like this:

我正在看这个,并认为我可以做一些类似的事情,这样我的数组现在看起来像这样:

[
  #<Activity _id: 4cea6c4572357e00fa00011a, created_at: 2010-11-22 13:12:37 UTC, updated_at: 2010-11-22 13:12:37 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea3ca172357e00fa000062, created_at: 2010-11-22 09:49:21 UTC, updated_at: 2010-11-22 09:49:21 UTC, action: "Attend", user_id: BSON::ObjectId('4cea39b772357e00fa000046'), artist_id: nil, media_id: BSON::ObjectId('4cea3c8b72357e00fa00005e')>
]

I'm not concerned which combination of results are extracted. As long as I have unique user_id's in the result set. Anyone know how this can be achieved?

我不关心提取了哪种结果组合。只要我在结果集中有唯一的 user_id。有谁知道这是如何实现的?

回答by Peter

You can just use the method uniq. Assuming your array is ary, call:

您可以只使用该方法uniq。假设您的数组是ary,请致电:

ary.uniq{|x| x.user_id}

and this will return a set with unique user_ids.

这将返回一个具有唯一user_ids的集合。

回答by ajmartin

This should work for you:

这应该适合你:

Consider Table1 has a column by the name of activitywhich may have the same value in more than one record. This is how you will extract ONLY the unique entries of activityfield within Table1.

考虑表 1 有一列以活动名称命名的列,该列可能在多个记录中具有相同的值。这就是您将如何仅提取表 1 中活动字段的唯一条目。

#An array of multiple data entries
@table1 = Table1.find(:all) 

#extracts **activity** for each entry in the array @table1, and returns only the ones which are unique 

@unique_activities = @table1.map{|t| t.activity}.uniq 

回答by xiy

For those hitting this up in the future, you can now use the Mongoid::Criteria#distinctmethod from Origin to select only distinct values from the database:

对于那些将来遇到此问题的人,您现在可以使用Mongoid::Criteria#distinctOrigin 中的方法从数据库中仅选择不同的值:

# Requires a Mongoid::Criteria
Attendees.all.distinct(:user_id)

http://mongoid.org/en/mongoid/docs/querying.html(v3.1.0)

http://mongoid.org/en/mongoid/docs/querying.html(v3.1.0)

回答by John Ballinger

Have you looked at this page?

你看过这个页面吗?

http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct

http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct

That might save you some time?

这可能会为您节省一些时间?

eg db.addresses.distinct("zip-code");

例如 db.addresses.distinct("zip-code");

回答by the Tin Man

Instead of using an Array, consider using either a Hashor a Set.

考虑使用HashSet,而不是使用 Array 。

Sets behave similar to an Array, only they contain unique values only, and, under the covers, are built on Hashes. Sets don't retain the order that items are put into them unlike Arrays. Hashes don't retain the order either but can be accessed via a key so you don't have to traverse the hash to find a particular item.

集合的行为类似于数组,只是它们仅包含唯一值,并且在幕后构建于哈希之上。与数组不同,集合不保留项目放入其中的顺序。散列也不保留顺序,但可以通过键访问,因此您不必遍历散列来查找特定项目。

I favor using Hashes. In your application the user_id could be the key and the value would be the entire object. That will automatically remove any duplicates from the hash.

我喜欢使用哈希。在您的应用程序中,user_id 可以是键,而值可以是整个对象。这将自动从哈希中删除任何重复项。

Or, only extract unique values from the database, like John Ballinger suggested.

或者,只从数据库中提取唯一值,就像 John Ballinger 建议的那样。

回答by Christian Fazzini

Errr, it's a bit messy in the view. But I think I've gotten it to work with group (http://mongoid.org/docs/querying/)

呃,视图有点乱。但我想我已经让它与组一起工作(http://mongoid.org/docs/querying/)

Controller

控制器

@event_attendees = Activity.only(:user_id).where(:action => 'Attend').order_by(:created_at.desc).group

View

看法

<% @event_attendees.each do |event_attendee| %>    
  <%= event_attendee['group'].first.user.first_name %>
<% end %>