[英]Array results using Active Record Query Model 'select' and 'where'
I have a user, membership and group model. 我有一个用户,成员资格和组模型。 A user has many memberships, and groups through memberships.
用户具有许多成员资格,并通过成员资格进行分组。 A group has many memberships, and users through memberships.
群组具有许多成员资格,并且用户通过成员资格。 Memberships belongs to both.
成员资格属于两者。
When a user accesses the users index view I want to show him/her all users that belong to the group he/she belongs to: I created the following code, which does not work. 当用户访问用户索引视图时,我想向他/她显示属于他/她所属组的所有用户:我创建了以下代码,该代码不起作用。 I cannot seem to get the query to work.
我似乎无法使查询工作。 Pease help!
请帮忙!
The code in the Users controller 用户控制器中的代码
class UsersController < ApplicationController
def index
@users = current_user.relevant_members
end
end
The code in the User model 用户模型中的代码
def relevant_members
group_ids = self.group_ids
user_ids = Membership.select("user_id").where("group_id IN (#{group_ids.join(", ")})")
User.find(user_ids)
end
I don't understand why, when tested in the console, user_ids is not an array of ids, but the following array: 我不明白为什么在控制台中进行测试时,user_ids不是ID数组,而是以下数组:
[#<Membership user_id: 2>, #<Membership user_id: 2>, #<Membership user_id: 3>, #<Membership user_id: 3>, #<Membership user_id: 3>, #<Membership user_id: 2>]
What would be a better way of returning an array of user ids to find all relevant group members? 返回用户ID数组以查找所有相关组成员的更好方法是什么?
Membership.select("user_id").where("group_id IN (#{group_ids.join(", ")})")
returns always an array of Membership objects, select("user_id") changes only the fields that are loaded from DB to the object. 总是返回Membership对象的数组,select(“ user_id”)仅更改从DB加载到对象的字段。
It's enough to map it to id 映射到id就足够了
user_ids = Membership.select("user_id").where("group_id IN (#{group_ids.join(", ")})").map(&:user_id)
Did you try the pluck() function ? 您是否尝试过pluck()函数? https://apidock.com/rails/ActiveRecord/Calculations/pluck
https://apidock.com/rails/ActiveRecord/Calculations/pluck
user_ids = Membership.pluck("user_id").where("group_id IN (#{group_ids.join(", ")})")
Membership.select('user_id')
will return an array of Membership
objects with just the user_id
attribute, that's correct so far. Membership.select('user_id')
将返回仅具有user_id
属性的Membership
对象数组,到目前为止是正确的。 It will work if you extract the ids from the result: 如果您从结果中提取ID,它将起作用:
membershipts = Membership.select("user_id").where("group_id IN (#{group_ids.join(", ")})")
user_ids = memberships.map(&:user_id)
Best regards 最好的祝福
Tobias 托比亚斯
This is because your user_ids
is an object of the class Membership
and hence you can't get an array of the 'user_ids' directly 这是因为您的
user_ids
是Membership
类的对象,因此您无法直接获取'user_ids'的数组
You have to do something like following 您必须执行以下操作
user_ids = Membership.select("user_id").where("group_id IN (#{group_ids.join(", ")})")
user_ids_array = user_ids.collect{|u| u.user_id}
User.find(user_ids_array)
Also you can modify your where clause as follow, which is more ruby way. 您也可以按照以下方式修改where子句,这是更有效的方法。
user_ids = Membership.select("user_id").where(["group_id IN (?)", group_ids])
Membership.where("group_id in (#{groups.collect(&:id).join(',')})")
.all.collect(&:user_id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.