I am trying to return all groups created by a user. All of the groups are associated with the user id. When I run a find_by query it only returns the first result. Is there a way for it to return multiple? Thanks in advance
我试图返回用户创建的所有组。所有组都与用户ID相关联。当我运行find_by查询时,它只返回第一个结果。有没有办法让它返回多个?提前致谢
4 个解决方案
#1
12
Change find_by to find_all_by and it will return all matching results.
将find_by更改为find_all_by,它将返回所有匹配的结果。
#2
18
I'm writing a separate answer because I can't comment on James Lowrey's answer as I don't have 50 points.
我正在写一个单独的答案,因为我无法评论James Lowrey的答案,因为我没有得到50分。
find_all_by is deprecated (Ruby 4.2).
不推荐使用find_all_by(Ruby 4.2)。
To get a list of active records from models, do:
要从模型中获取活动记录列表,请执行以下操作:
Model.where(attribute_name: val)
For example, to find all records in Vehicle table (having column name "model_name") such that the value of model_name is "Audi", do
例如,要查找Vehicle表中的所有记录(具有列名“model_name”),使得model_name的值为“Audi”,请执行
@vehicles = Vehicle.where(model_name: "Audi")
You can iterate through these like so:
您可以像这样迭代这些:
<% @vehicles.each do |vehicle| %>
#3
2
I think find_all_by may be deprecated now (at least, I couldn't get it to work). I believe the 'where' function is now recommended
我认为find_all_by现在可能已被弃用(至少,我无法让它工作)。我相信现在推荐'where'功能
where("name LIKE ?","%#{search}%")
where(instructor_id: params[:choosen_instructor_id])
where ("id = ?",id_val)
In case you didn't know, the ? and parameter list is to prevent SQL injections
万一你不知道,?和参数列表是为了防止SQL注入
#4
0
User class has id as primary key.
which is stored as user_id in Group class
Then to find all groups associated with that user could be found as.
@groups=Group.find_all_by_user_id(user_id)
用户类将id作为主键。在Group类中存储为user_id然后查找与该用户关联的所有组可以找到。 @基团= Group.find_all_by_user_id(USER_ID)
#1
12
Change find_by to find_all_by and it will return all matching results.
将find_by更改为find_all_by,它将返回所有匹配的结果。
#2
18
I'm writing a separate answer because I can't comment on James Lowrey's answer as I don't have 50 points.
我正在写一个单独的答案,因为我无法评论James Lowrey的答案,因为我没有得到50分。
find_all_by is deprecated (Ruby 4.2).
不推荐使用find_all_by(Ruby 4.2)。
To get a list of active records from models, do:
要从模型中获取活动记录列表,请执行以下操作:
Model.where(attribute_name: val)
For example, to find all records in Vehicle table (having column name "model_name") such that the value of model_name is "Audi", do
例如,要查找Vehicle表中的所有记录(具有列名“model_name”),使得model_name的值为“Audi”,请执行
@vehicles = Vehicle.where(model_name: "Audi")
You can iterate through these like so:
您可以像这样迭代这些:
<% @vehicles.each do |vehicle| %>
#3
2
I think find_all_by may be deprecated now (at least, I couldn't get it to work). I believe the 'where' function is now recommended
我认为find_all_by现在可能已被弃用(至少,我无法让它工作)。我相信现在推荐'where'功能
where("name LIKE ?","%#{search}%")
where(instructor_id: params[:choosen_instructor_id])
where ("id = ?",id_val)
In case you didn't know, the ? and parameter list is to prevent SQL injections
万一你不知道,?和参数列表是为了防止SQL注入
#4
0
User class has id as primary key.
which is stored as user_id in Group class
Then to find all groups associated with that user could be found as.
@groups=Group.find_all_by_user_id(user_id)
用户类将id作为主键。在Group类中存储为user_id然后查找与该用户关联的所有组可以找到。 @基团= Group.find_all_by_user_id(USER_ID)