Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails: Is there a way to retrieve _idx property of an Object in a List?

Tags:

list

grails

I have an association like this

class Parent 
List children

static hasMany =[children:Child]

I need to be able to know the order of a Child object when I look at it outside of Parent context. So that I can tell if it is a 1st child, second child, etc.

like image 234
dbrin Avatar asked Oct 08 '11 00:10

dbrin


1 Answers

What do you mean with 'outside of Parent context'? To get index of a object in a list - you need to load it before. If you want to get index without loading Parent, then you have to use an raw SQL for this, but i'm not sure that it will be faster than loading Parent, because it's the same logic, except making mapping resultset to a model.

Btw, to get this index when you have Parent instance, you have to use:

int idx = parent.children.indexOf(child)

And don't forget to implement .equals of your Child domain.

like image 148
Igor Artamonov Avatar answered Nov 17 '22 07:11

Igor Artamonov