Ruby on Rails:搜索一个表,其中另一个表中必须存在多行

发布于 2025-01-04 06:22:58 字数 982 浏览 2 评论 0原文

我正在尝试创建一个搜索,其中单个记录必须在另一个表中具有多个记录(由 id 和 has_many 语句链接)才能包含在结果中。

我有表usersskill_listsskill_maps

用户通过 skill_maps 表中的单个条目映射到个人技能。许多用户可以共享一项技能,并且单个用户可以通过 skill_maps 表中的多个条目拥有多种技能。

例如,

User_id | Skill_list_id
2       | 9
2       | 15
3       | 9

用户 2 拥有技能 9 和 15
用户 3 只有技能 9

我正在尝试创建一个搜索,返回具有一组技能的所有用户的哈希值。所需的 skill_ids 集在参数中显示为数组。

这是我正在使用的代码:

skill_selection_user_ids = SkillMap.find_all_by_skill_list_id(params[:skill_ids]).map(&:user_id) 

@results = User.find(:all, :conditions => {:id => skill_selection_user_ids})

问题是,这会返回具有任何这些技能的所有用户,而不是具有所有这些技能的用户。

另外,我的用户表链接到 Skill_lists 表 :through => :skill_maps 反之亦然,这样我就可以调用 @user.skill_list 等...

我确信这是一个真正的新手问题,我对 Rails (和编程)完全陌生)。我搜索并寻找解决方案,但找不到任何东西。我真的不知道如何用一个搜索词来解释这个问题。

I'm trying to create a search where a single record must have multiple records in another table (linked by id's and has_many statements) in order to be included as a result.

I have tables users, skill_lists, skill_maps.

users are mapped to individual skills through single entries in the skill_maps table. Many user can share a single skill and single user can have many skills trough multiple entries in the skill_maps table.

e.g.

User_id | Skill_list_id
2       | 9
2       | 15
3       | 9

user 2 has skills 9 and 15
user 3 has only skill 9

I'm trying to create a search that returns a hash of all users which have a set of skills. The set of required skill_ids appear as an array in the params.

Here's the code that I'm using:

skill_selection_user_ids = SkillMap.find_all_by_skill_list_id(params[:skill_ids]).map(&:user_id) 

@results = User.find(:all, :conditions => {:id => skill_selection_user_ids})

The problem is that this returns all users that have ANY of these skills not users that have ALL of them.

Also, my users table is linked to the skill_lists table :through => :skill_maps and visa versa so that i can call @user.skill_list etc...

I'm sure this is a real newbie question, I'm totally new to rails (and programming). I searched and searched for a solution but couldn't find anything. I don't really know how to explain the problem in a single search term.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜警司 2025-01-11 06:22:58

我个人不知道如何使用 ActiveRecord 的查询接口来做到这一点。最简单的事情是检索具有每种技能的用户列表,然后获取这些列表的交集,也许使用 Set:

require 'set'

skills = [5, 10, 19] # for example
user_ids = skills.map { |s| Set.new(SkillMap.find_all_by_skill_list_id(s).map(&:user_id)) }.reduce(:&)
users = User.where(:id => user_ids.to_a)

为了(可能)更高的性能,您可以“滚动自己的”SQL 并让数据库发动机做工作。如果您需要高性能,我也许可以为您提供一些 SQL。 (或者如果其他人可以,请编辑此答案!)

顺便说一句,您可能应该在 skill_maps.skill_list_id 上放置一个索引,以确保良好的性能,即使 skill_maps 表变得非常大。请参阅 ActiveMigration 文档:http://api.rubyonrails.org/classes/ActiveRecord/Migration。 html

I personally don't know how to do this using ActiveRecord's query interface. The easiest thing to do would be to retrieve lists of users who have each individual skill, and then take the intersection of those lists, perhaps using Set:

require 'set'

skills = [5, 10, 19] # for example
user_ids = skills.map { |s| Set.new(SkillMap.find_all_by_skill_list_id(s).map(&:user_id)) }.reduce(:&)
users = User.where(:id => user_ids.to_a)

For (likely) higher performance, you could "roll your own" SQL and let the DB engine do the work. I may be able to come up with some SQL for you, if you need high performance here. (Or if anyone else can, please edit this answer!)

By the way, you should probably put an index on skill_maps.skill_list_id to ensure good performance even if the skill_maps table gets very large. See the ActiveMigration documentation: http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

比忠 2025-01-11 06:22:58

您可能需要使用一些自定义 SQL 来获取用户 ID。我在类似的 HABTM 关系上测试了这个查询,它似乎有效:

SELECT DISTINCT(user_id) FROM skill_maps AS t1 WHERE (SELECT COUNT(skill_list_id) FROM skill_maps AS t2 WHERE t2.user_id = t1.user_id AND t2.skill_list_id IN (1,2,3)) = 3

技巧在于子查询。对于外部查询中的每一行,它会查找该行与您感兴趣的任意技能相匹配的记录数。然后检查是否 >该计数与您感兴趣的技能的总数相匹配。如果匹配,则用户必须拥有您搜索的所有技能。

您可以使用 find_by_sql 在 Rails 中执行此操作:

sql = 'SELECT DISTINCT(user_id) FROM skill_maps AS t1 WHERE (SELECT COUNT(skill_list_id) FROM skill_maps AS t2 WHERE t2.user_id = t1.user_id AND t2.skill_list_id IN (?)) = ?'
skill_ids = params[:skill_ids]
user_ids = SkillMap.find_by_sql([sql, skill_ids, skill_ids.size])

抱歉,如果表名和列名不完全正确,但希望这是大致正确的。

You'll probably have to use some custom SQL to get the user IDs. I tested this query on a similar HABTM relationship and it seems to work:

SELECT DISTINCT(user_id) FROM skill_maps AS t1 WHERE (SELECT COUNT(skill_list_id) FROM skill_maps AS t2 WHERE t2.user_id = t1.user_id AND t2.skill_list_id IN (1,2,3)) = 3

The trick is in the subquery. For each row in the outer query, it finds a count of records for that row that match any of the skills that you're interested in. Then it checks whether that count matches the total number of skills you're interested in. If there's a match, then the user must possess all of the skills you searched for.

You could execute this in Rails using find_by_sql:

sql = 'SELECT DISTINCT(user_id) FROM skill_maps AS t1 WHERE (SELECT COUNT(skill_list_id) FROM skill_maps AS t2 WHERE t2.user_id = t1.user_id AND t2.skill_list_id IN (?)) = ?'
skill_ids = params[:skill_ids]
user_ids = SkillMap.find_by_sql([sql, skill_ids, skill_ids.size])

Sorry if the table and column names aren't exactly right, but hopefully this is in the ballpark.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文