Sap
Sap

Reputation: 5291

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?

Upvotes: 1

Views: 116

Answers (1)

tim_yates
tim_yates

Reputation: 171084

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

Upvotes: 3

Related Questions