Search code examples
grailsgrails-orm

GORM Basic undestandig problems


I am trying to understand how GORM works. Building a Database for projects with buildings and buildingparts(areas of a great building) doesn`t work the way I want it to. What I want is: each project has one building and one of the buildingpart of that building.

I tried it that way:

class Project {
    String name
    static hasOne = [building: Building]
    Buildingpart buildingpart
    String toString(){
        return name
    }
}

class Building {
    String name
    static hasMany = [projects: Project, buildingparts: Buildingpart]
    String toString(){
        return name
    }
}

class Buildingpart {
    String buildingpart
    static belongsTo = [building: Building]
    String toString(){
        return buildingpart
    }
}

Doing it that way gives me a dropdownmenu for every buildingpart and not only the ones from the selectet building. I would be happy for any suggestions.


Solution

  • Design looks ok You probably need an additional hook in the Building class something like

    Buildingpart getCurrentPart(Buildingpart buildingPart) {
    return  this.buildingparts.find{buildingPart}
    }
    

    Then

    Project project = project.get(id)
    
    
    
     println "project building = ${project.building}"
       println "project buildingparts = ${project.building.buildingparts}"
       println "project currentpart = ${project.building.getCurrentPart(project.buildingpart)}"
    

    When you say your select box, this makes no sense to me since to us here we have no idea what that all means Also a note in your project class it is called :

    Bauteil buildingpart

    then you have hasMany buildingparts: Buildingpart]

    The thing you need to pay attention to is the binding happening in println and shown below:

    when you then query the buiding many parts for the object from project

    project.building.getCurrentPart(project.buildingpart)