Reputation: 2108
how can I get Collection of specific Class?
I use inheriance: On Planet live Human. Humans are dividing to Men and Women.
class Planet{
String name
static hasMany = [ humans : Human ]
}
class Human{
String name
static belongsTo = [Planet]
}
class Man extends Human{
int countOfCar
}
class Woman extends Human{
int coutOfChildren
}
now a neet to get only Collection of Man or Collection of Woman:
get all humans on planet is simple
all = Planet.get(1).humans
but what can I get only woman or men?
womenLivedOnMars = Planet.get(1).getOnlyWoman
menLivedOnJupiter = Planet.get(2).getOnlyMan
Thanks for your help
Tom
Upvotes: 2
Views: 2295
Reputation: 26152
As far as I know you can do it these ways:
class Human{
String name
static belongsTo = [planet: Planet]
}
womanLivedOnMars = Woman.findAllByPlanet(Planet.findByName('Mars'))
menLivedOnJupiter = Men.withCriteria {
planet {
eq('name','Mars')
}
}
Upvotes: 2
Reputation: 2860
A simple option could be:
Man.findAllByPlanet(Planet.get(1))
Upvotes: 2