简体   繁体   English

在Rails中,为什么我希望自己的finder方法只返回一个结果呢?

[英]In rails, why is my finder method only returning one result when I want it to return all of them?

I'm using Rails 5.0.3. 我正在使用Rails 5.0.3。 How do I find all matching records using a finder? 如何使用查找器查找所有匹配的记录? I have 我有

my_obj = self.find_by_name_and_day_and_user_id(name, day, user_id)

but it returns only a single result. 但它只返回一个结果。 When I run turn on the SQL, it is adding a 当我运行时打开SQL,它添加了一个

 LIMIT 1

clause. 条款。 How do I write a finder method that will return all results and not just one? 如何编写一个查找程序方法,该方法将返回所有结果,而不仅仅是一个?

您应该使用where ,就像这样

self.where(name: name, day: day, user_id: user_id)

find_by method always return first matching record. find_by方法始终返回第一条匹配记录。 So you should use where clause which will return all matching records. 因此,您应该使用where子句,该子句将返回所有匹配的记录。

my_obj = self.where(name: name, day: day, user_id: user_id)

暂无
暂无

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

相关问题 为什么当我有多个结果时,此powershell代码仅返回一个结果? - Why is this powershell code only returning one result when I have multiple results? 当我只想要一个时聚合查询返回多行 - Aggregate query returning multiple rows when I only want one 我的返回结果是返回除一个之外的所有正确答案,没有错误 - My return-result is returning all the correct answers except one, with not errors 带有 LIKE 的 Oracle SQL CASE 为两种情况返回一个值,当我只想要结果时,如果它们具有任何条件 - Oracle SQL CASE with a LIKE returning a value for both scenarios when I only want a result if they have any of the criteria Mysql:按月搜索数据只会产生一个结果,而不是全部 - Mysql: Searching for data by month only yields one result not all of them 为什么当我希望MySQL查询全部显示时,它会省略记录? - Why does MySQL query omit records when I want it to show them all? 在rails中,如何从表中选择一个特定列的数据? (只有一行,所以我只想要一个特定的结果,但不是一个数组) - In rails, how to select one specific column's data from a table? (there is only 1 row so I want only a specific result but not an array) 当我只想要一行时,SQL JOIN 返回多行 - SQL JOIN returning multiple rows when I only want one row SQL查询仅返回一个结果/行,而不是全部 - SQL Query is only returning one result/row rather than all 仅当组中的所有记录均符合条件时才分组并返回结果 - Grouping and returning result only when all records in the group match the criteria
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM