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
- belong to a certain user
- have
:target_date == Date.today
(target_date is a column in the day_tasks table).
I want to put the results into the @day_tasks array.
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?
find
is supposed to return the first element found whilefind_all
returns all of them, may be worth trying iffind_all
works on your case.find_all
is deprecated in Rails 3, replaced byfind(:all)
. A better solution is to usewhere
, as shown in my answer.