Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6672545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:31:16+00:00 2026-05-26T03:31:16+00:00

I have models as follows: User has_many goals, Goal has_many tasks, Task has_many day_tasks.

  • 0

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

  1. belong to a certain user
  2. 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:31:17+00:00Added an answer on May 26, 2026 at 3:31 am

    You wrote this:

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

    The find method 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 or will return nil.

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

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

    The joins method here will join the tables that match the association names in your DayTask model and related models. This means you must have a task association on the DayTask model and on that model have a goals association and on the goals model have one for user.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four models: User Award Badge GameWeek The associations are as follows: User
I have two models, User and TrainingSession: class User < ActiveRecord::Base has_many :training_sessions end
I have a two models, Group and User User belongs_to Group and Group has_many
I have the follow models: User has_many :armies Army belongs_to :user My controller with
I have four models in my application, defined as follows class User < ActiveRecord::Base
I have two models related as follows. USERS has_many :celebrations has_many :boards, :through =>
I have a Repository model as follows: class Repository < ActiveRecord::Base belongs_to :user has_many
I have a django app with models as follows: A Question model An Answer
I have three models of concern here: User Fight FightPunches Punches The associations are
Right now, I have three models Post, Comment and User (using Devise ) associated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.