Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何获取此数组中的唯一元素?_Ruby On Rails_Ruby - Fatal编程技术网

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

Ruby on rails 如何获取此数组中的唯一元素?,ruby-on-rails,ruby,Ruby On Rails,Ruby,使用Mongoid。不幸的是,Mongoid不允许选择unique/distinct! 我们已经得到了这些结果。如您所见,共有7个结果。如果仔细查看用户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::ObjectI

使用Mongoid。不幸的是,Mongoid不允许选择unique/distinct! 我们已经得到了这些结果。如您所见,共有7个结果。如果仔细查看用户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')>
] 
我在看,并且在想我可以做一些类似的事情,这样我的阵列现在看起来像这样:

[
  #<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')>
]

我不关心提取哪种结果组合。只要我在结果集中有唯一的用户id。有人知道这是如何实现的吗?

< P>而不是使用数组,考虑使用A或./P> 集合的行为类似于数组,只是它们只包含唯一的值,而且在幕后是基于散列构建的。与数组不同,集合不保留项放入集合的顺序。散列也不保留顺序,但可以通过键访问,因此不必遍历散列来查找特定项

我喜欢使用散列。在应用程序中,用户id可以是键,值可以是整个对象。这将自动从散列中删除任何重复项


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

您可以使用uniq方法。假设您的数组是ary,请调用:


这将返回一个具有唯一用户ID的集合。

您看过这页了吗

那可能会节省你一些时间

乙二醇
db.addresses.distinctzip-code

呃,视野有点乱。但我想我已经和团队合作了http://mongoid.org/docs/querying/

控制器

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


这应该适合您:

假设表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 

对于将来遇到此问题的用户,现在可以使用Mongoid::Criteriadistinct方法从源代码中仅选择不同的数据库值:

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

v3.1.0

这是OP问题的快速解决方案,但从长远来看,会鼓励对所有数据进行SLURP,然后对其进行迭代以删除重复数据。这将消耗更多的资源。一个更好的解决方案是OP使用一个更智能的查询来预先消除重复,或者使用一个集合类型来根据其性质消除重复。@Greg:是的,我同意,但请确保您也没有进行过早的优化……这不是过早的,这是30多年处理数据库的经验。@Peter它返回的是一个数组而不是一个集合。请参阅Ruby 1.9中的注意事项,哈希的顺序是保持不变的,因此我认为是集合。如果我在后者上错了,我很高兴被纠正。
#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 
# Requires a Mongoid::Criteria
Attendees.all.distinct(:user_id)