Search code examples
grailsgrails-orm

Grails returning one element from each object


I am trying to just return a single string from each object.

Given the following:

 class Book {
String title
Date releaseDate
String author
Boolean paperback
}

for every instance of Book I want to get an array of authors then make them unique. I thought you could do something like:

def authors =  Book.findAllByAuthor()

This just gives me an array off book objects. I know i can do a

a =[]
authors.each{a.add(it.author)}
a.unique()

I am almost certain there is a way just to grab all authors in one line.

any ideas?


Solution

  • This gives you distinct authors of any book:

      Book.executeQuery("select distinct author from Book")