Search code examples
grailsgroovygspone-to-one

Grails order by for 1 to Many related objects


In grails GSP I am doing something like

 <g:each in="${testInstance?.testConfigs?}" var="t">

Two classes are

class Test{
    static hasMany = [testConfigs:TestConfig]
}

class TestConfig {
    Date dateCreated
    Date lastUpdated
    Test test
    static constraints = {
        dateCreated display:false
        lastUpdated display:false
        questionType inList:["Fill in the blanks","Multipl choice","True False"]
    }
}

How can I change the for each loop so that testConfigs objects are retrieved ordered descending by dateCreated field?


Solution

  • Does defining a sort order like it says in the docs not work?

    class Test{
        static hasMany = [testConfigs:TestConfig]
    
        static mapping = {
            testConfigs sort: 'dateCreated', order: 'desc'
        }
    }