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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:49:40+00:00 2026-05-31T01:49:40+00:00

I have a Project class like this : class Project { static hasMany =

  • 0

I have a Project class like this :

class Project {
    static hasMany = [ tasks : Tasks , users : User ]
    static belongsTo = User
    String name
    String toString() {
        this.name
    }
}

User class like this :

class User {
    static hasMany = [ project: Project ]
    Profile profile
    String username
    String password
    String toString() {
        this.username
    }
}

And Tasks class like this :

class Tasks {
    static belongsTo = [ user : User, project: Project ]
    boolean completed
    String toString(){
       this.title
    }
}

User will be having many Project and Project will be having many Tasks.

Now I need a way to query the DB to find, all the Tasks that are completed and that belongs to a Project. That is for example say that there are two projects, say P1 and P2. In which P1 has 4 completed Tasks and P2 has 1 completed Tasks. My code need to return something like :

[P1:[t1,t2,t3,t4],P2:[t5]]

I know its easy to find all the Tasks that are completed, which will return like this :

[t1,t2,t3,t4,t5]

But I find it difficult to group them according to their Project.

How can I do this?

Thanks in advance.

  • 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-31T01:49:41+00:00Added an answer on May 31, 2026 at 1:49 am

    You can use the groupBy on Collection: API or EXAMPLE

    For example:

    def completedTasks = Task.findAllByCompleted(true)
    def compltedTasksByProject = compltedTasks.groupBy {it.project}
    

    Should give you what you want.

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

Sidebar

Related Questions

In my Grails 1.3.7 project I have a domain class like this: class User
I have a Project model similar to: class Project(models.Model): ... lead_analyst=models.ForeignKey(User, related_name='lead_analyst') ... I
Hope I'm asking this correctly: I have a project Projects.Client I have my class
Okay so, I have this project structure: package A.B class SuperClass (this class is
Suppose I have a class User with many Customers (marked with hasMany property). In
i have something like this in my project, the project it's kinda finished already
Usually I'm using One-to-many relationship by this way : class Study { static hasMany
So I have a class that looks like this: public class MyClassToTest() { MyStaticClass.DoSomethingThatIsBadForUnitTesting();
Let's have this class class A { protected static function ident() { return I
I have a model like this: class Item(models.Model): code = models.CharField(max_length=200, unique=True) barcode =

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.