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 4008414
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:45:07+00:00 2026-05-20T08:45:07+00:00

My Rails db scheme has projects and task. I want to show projects which

  • 0

My Rails db scheme has projects and task. I want to show projects which have at least one open task. This is my code:

class Project
    scope :open_tasks, lambda {
        where(:tasks => {:finished => false}).includes(:tasks)
    }
    ...
end

This code correctly returns the projects with one open task, but only with the one open tasks and not all. e.g. a project has 5 tasks total and 2 open task, the code from above would return only the project with 2 tasks. I know that I could simply force to reload the project, but this is very hackish and has performance problems.
How can I get the project with all tasks?

  • 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-20T08:45:08+00:00Added an answer on May 20, 2026 at 8:45 am

    The where condition is always going to limit which associated tasks are returned.

    It sounds like you want to return the projects and all of their associated tasks regardless of status if at least one of those tasks is not finished?

    You could try this (I don’t think you need the lambda since you’re not using any variables/times/etc. in your scope). It assumes you have a has_many :tasks and belongs_to :project. You’d need to tweak it if you’re using a has_many :through, etc.

    scope :open_tasks, where("(select count(*) from tasks where tasks.project_id=projects.id) > 0").includes(:tasks)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[Rails] Where to put this code? I have a user1 and when another registered
I want to access a legacy database schema from Rails. I have one table
Rails has a nice idiom that makes it easy for you to have a
Rails has the rest autho plugin which works well but is there a solution
I have a Rails 2.3 app which tracks an organisation's members, and whether they
I've never had anything but success w/ Rails migrations, so this one is especially
I've been looking to read some ruby code(specifically Rails) but I don't want to
I have a Rails application, I have the following DB schema: each User has
A rails app I am developing currently has about 35 migrations up to this
I'm a complete rails newbie, so forgive me if this is trivial. I have

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.