Search code examples
grailsdrop-down-menugrails-orm

GORM: how to retrieve only two values for dropdown


In Grails using GORM, I'd like to retrieve two possible values for a form dropdown. This particular instance is to only have two possible countries in a dropdown. I've set them in my Config.groovy

The GORM statement in this that I've done only returns USA and I'd like to return Canada also - so I have the findAll statement slightly incorrect. Can someone help me?

Country<g:select name="Country"  from="${....country.findAllById("100225","100038").sort{it.orderNumber}}" value="otherstuff" class="form-control" required="" aria-labelledby="country-label"/>

Config.groovy:

country.usa=100225
country.canada=100038

Domain class:

class country {

    String name
    String value
    int orderNumber = 0

    static constraints = {
        name nullable:false, maxSize:50, blank:false
        value nullable:false, maxSize:100, blank:false
    }

    String toString(){
        "$name - $value"
    }

    static mapping = {
        table 'country'
        cache: 'read-write'
        columns{
            id column:'id'
            name column:'name'
            value column:'value'
            orderNumber column:'order_number'
        }
        id generator: 'assigned'
    }
}

Solution

  • you should rather use findAllByIdInList(["100225","100038"]).

    Also consider not writing such code within the view. Make it part of your model and prepare it in the controller.