Ruby '.find' 方法只返回第一次出现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7654613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:06:24  来源:igfitidea点击:

Ruby '.find' method only returning first occurrence

ruby-on-railsruby

提问by Grant David Bachman

I have models as follows: User has_many goals, Goal has_many tasks, Task has_many day_tasks. I'm trying to write a method which finds all day_tasks that

我有如下模型:用户 has_many 目标,目标 has_many 任务,任务 has_many day_tasks。我正在尝试编写一个方法来查找所有 day_tasks

  1. belong to a certain user
  2. have :target_date == Date.today(target_date is a column in the day_tasks table).
  1. 属于某个用户
  2. :target_date == Date.today(target_date 是 day_tasks 表中的一列)。

I want to put the results into the @day_tasks array.

我想将结果放入@day_tasks 数组中。

my code:

我的代码:

@user = current_user
@day_tasks = DayTask.find { |x| x.task.goal.user == @user && x.target_date == Date.today }

This code only returns the first record that matches these criteria. I've also tried using the DayTasks.where method with the same code in the braces, but I just a "Wrong number of arguments ( 0 for 1 )" error. Could someone explain why my method only returns the first occurrence and what exactly the difference is between .find and .where?

此代码仅返回符合这些条件的第一条记录。我也试过使用 DayTasks.where 方法在大括号中使用相同的代码,但我只是一个“参数数量错误(0 代表 1)”错误。有人可以解释为什么我的方法只返回第一次出现以及 .find 和 .where 之间的区别是什么?

回答by Ryan Bigg

You wrote this:

你写了这个:

@day_tasks = DayTask.find { |x| x.task.goal.user == @user && x.target_date == Date.today }

The findmethod here is actually falling back to Enumerable's find, which is an alias for detect, which takes a block and will return the first element in that collection that matches the block's conditions orwill return nil.

find这里的方法实际上是回退到Enumerable's find,它是 的别名detect,它接受一个块并返回该集合中与块的条件匹配将返回的第一个元素nil

In order to fix this, you're going to need to use ARel's query stuff that's built-in to ActiveRecord.

为了解决这个问题,您将需要使用 ActiveRecord 内置的 ARel 查询内容。

DayTask.joins(:task => { :goals => :user }).where("users.id = ? AND day_tasks.target_date = ?", @user.id, Date.today)

The joinsmethod here will join the tables that match the associationnames in your DayTaskmodel and related models. This means you must have a taskassociation on the DayTaskmodel and on that model have a goalsassociation and on the goals model have one for user.

joins此处的方法将连接与您的模型和相关模型中的关联名称匹配的表DayTask。这意味着您必须taskDayTask模型上有一个goals关联,并且在该模型上有一个关联,并且在目标模型上有一个 for user

The wherewill then construct an SQL condition that will query the joins to find all records that belong to a user and have a target_dateof today.

然后where将构造一个 SQL 条件,该条件将查询连接以查找属于某个用户并具有target_date今天的所有记录。

回答by Tilo

please check the ActiveRecord Query Interface for Rails 3.x :

请检查 Rails 3.x 的 ActiveRecord 查询接口:

http://m.onkey.org/active-record-query-interface

http://m.onkey.org/active-record-query-interface

http://guides.rubyonrails.org/active_record_querying.html

http://guides.rubyonrails.org/active_record_querying.html

you'll probably want find(:all, ...) or where()

你可能想要 find(:all, ...) 或 where()