tags:

views:

31

answers:

1

Hi, I have a this model :

class Question{
  Set components
  static hasMany = [components: QuestionComponent]
}

class QuestionComponent{
  static belongsTo = Question
}

class QuestionComponentStatus extends QuestionComponent{

}
class QuestionComponentOther extends QuestionComponent{

}

I want to get only QuestionComponentStatus from Set components :

questionInstance.components. ?

Thanks a lot

+1  A: 

You can just do a query directly on the subclass to avoid polymorphic results. Provided that your one-to-many relationship is bi-directional (i.e. static belongsTo = [question: Question]), you could do something like:

QuestionComponentStatus.findAllByQuestion(q)

or in HQL:

QuestionComponentStatus.findAll("FROM QuestionComponentStatus WHERE question = :question", [question: q])
Daniel Rinser
Ok, I do that but is it not possible to do something like :questionInstance.components<QuestionComponentStatus>.each{...}Thanks
No, I guess not. You can of course do something like `questionInstance.components.findAll { it.class == QuestionComponentStatus.class }`, but this will retrieve all components from the DB and then filter them afterwards. As an alternative, you can make the interface nicer by introducing a custom getter for the filtered component lists, eg. `def getStatusComponents() { QuestionComponentStatus.findAllByQuestion(this) }` inside your `Question` class.
Daniel Rinser