Badmiral
Badmiral

Reputation: 1589

GORM query to find the most recent of an entry

New to how the ORM works so bear with me. I have a domain class

class MyClass()
{
    String myName
    Date mydate
}

I have bootstrapped a couple of examples with the same string and different dates. Then in my gsp I do an ajax call to a controller method

def MyAjaxCall
{
    def classes = MyClass.findAll()
}

How can I change this so I only return unique myName that have the most recent mydate? Thanks

Upvotes: 2

Views: 2621

Answers (2)

uchamp
uchamp

Reputation: 2512

Try something on the following lines:

def classes = MyClass.createCriteria().list {
    projections {
        property("id")
        groupProperty("myName")
        max("mydate")
    }
}

and then get MyClass instances:

def latestClasses = results?.collect{MyClass.read(it[0])}

Upvotes: 6

Dopele
Dopele

Reputation: 577

def classes = MyClass.withCriteria {
    projections {
        property "myName"
    }
    maxResults(1)
    order("mydate", "desc")
}

Upvotes: 6

Related Questions