groovy - How do I group the results in Grails? -


i have project class :

class project {     static hasmany = [ tasks : tasks , users : user ]     static belongsto = user     string name     string tostring() {         this.name     } } 

user class :

class user {     static hasmany = [ project: project ]     profile profile     string username     string password     string tostring() {         this.username     } } 

and tasks class :

class tasks {     static belongsto = [ user : user, project: project ]     boolean completed     string tostring(){        this.title     } } 

user having many project , project having many tasks.

now need way query db find, tasks completed , belongs project. example there 2 projects, p1 , p2. in p1 has 4 completed tasks , p2 has 1 completed tasks. code need return :

[p1:[t1,t2,t3,t4],p2:[t5]] 

i know easy find tasks completed, return :

[t1,t2,t3,t4,t5] 

but find difficult group them according project.

how can this?

thanks in advance.

you can use groupby on collection: api or example

for example:

def completedtasks = task.findallbycompleted(true) def compltedtasksbyproject = compltedtasks.groupby {it.project} 

should give want.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -