Search code examples
grailsgroovygrails-orm

Grails - dynamic finders derived date query GORM


I have the following domain class

class Book {
    String title
    Date releaseDate
    Integer daysOnShelf
    Author author
}

How can I list all books where current date is greater than the releaseDate + daysOnShelf? For instance, don't list if 2015-02-10 + 5 (releaseDate + daysOnShelf) since date is less than now.

Can this be done using GORM dynamic finders or Criteria Builder ? e.g.,

 def index(Integer max) {
     def books = Book.findAll {
         releaseDate.plus(daysOnShelf) < new Date()
     }
     respond books
 }

Solution

  • This should acchieve what you want:

    Date dateMinusDaysOnShelf = new Date() - daysOnShelf
    
    Book.findAllByReleaseDateLessThan(dateMinusDaysOnShelf)