简体   繁体   English

在Rails中配对所有用户的最佳方法?

[英]Best way to pair off all users in Rails?

I currently have a DB with a large group of users, and am looking for a way to pair them all up, based on a certain number of things. 我目前有一个拥有大量用户的数据库,并且正在基于某种情况寻找一种将它们配对的方法。

What is the best way to randomly iterate through the db of users, and pair them all up with each other? 随机迭代用户数据库并将它们彼此配对的最佳方法是什么? Ensuring that nobody misses out, and that time you do so everything is randomized. 确保没有人错过,并且每次您这样做时,一切都是随机的。

Has anyone had any experience with this before? 以前有人对此有经验吗? Any help would be much appreciated. 任何帮助将非常感激。

There are probably lots of ways to do this, but if the database can handle a random query you could grab all the users like this (where RANDOM() is your DB's random function) 可能有很多方法可以做到这一点,但是如果数据库可以处理随机查询,您可以像这样抓住所有用户(其中​​RANDOM()是数据库的随机函数)

User.order('RANDOM()').in_groups_of(2) do |a,b|
  # code to match user a to user b
end

You'd have to deal with an odd number of users and the above isn't efficient, but it will work. 您将不得不处理数量奇数的用户,并且上述方法效率不高,但可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM