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

  • Home
  • SEARCH
  • 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

A rails app I am developing currently has about 35 migrations up to this
I have a couple of objects in a Rails app (Ticket, and Comment) class
Rails comes with a handy session hash into which we can cram stuff to
Rails introduced some core extensions to Ruby like 3.days.from_now which returns, as you'd expect
Rails is a great platform, but it just doesn't have the history that Perl
Using Rails v2.1, lets say you have an action for a controller that is
In which I mean rebasing in the dictionary, rather than git definition... I have
I am a little confused about the auto-increment id field in rails. I have
I'm using Ruby on Rails to develop a web application. I have a wiki
I'm attempting to design an achievement system in Ruby on Rails and have run

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.