Search code examples
grailsgroovygrails-orm

Order is not working with criteria in Grails


I have just started working groovy and grails and has some confusion on Order clause with criteria.

Below is my code,

def order = "rollNumber"
def orderBy = "asc"


studentListCriteria = Students.createCriteria()

int max = 6
int offset = params?.offset ? Integer.valueOf(params.offset) : 0

def studentList = studentListCriteria.list(max: max, offset: offset) {
        and {
            eq("isActive", Boolean.TRUE )
        }
        order(order,orderBy)
    }

While executing it's throw below error.

groovy.lang.MissingMethodExceptionMessageNo signature of method: java.lang.String.call() is applicable for argument types: (java.lang.String, java.lang.String) values: [rollNumber, asc] Possible solutions: wait(), any(), tr(java.lang.String, java.lang.String), trim(), find(), size()   

Can anyone suggest what am i doing wrong here??


Solution

  • Please check below Code, You can not use Keywords here.

    def orderParameter = "rollNumber"//Change name of variable.
    def orderByParameter = "asc"
    
    
    studentListCriteria = Students.createCriteria()
    
    int max = 6
    int offset = params?.offset ? Integer.valueOf(params.offset) : 0
    
    def studentList = studentListCriteria.list(max: max, offset: offset) {
            and {
                eq("isActive", Boolean.TRUE )
            }
            order(orderParameter,orderByParameter)
        }
    

    Here, you are using order as a variable. Please change the name of it and good to go.