tags:

views:

35

answers:

3

Hi gurus

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

+1  A: 

A simple option could be:

Man.findAllByPlanet(Planet.get(1))
Daniel Engmann
+1  A: 

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')
    }
}
Max