tags:

views:

82

answers:

2

Is there a shorter/cleaner way than DomainClass.findAll()[0] to retrieve the first domain object in the set of domain objects that would normally be retrieved by findAll()?

Ideally, I'd like DomainClass.find() but such a finder does not exist.

+1  A: 

How about using list:

DomainClass.list(max:1)
seth
seth: Since .list() returns a List wouldn't that just result in the code DomainClass.list(max:1)[0]? What I would like is something a long the line "DomainClass foo = DomainClass.find()".
knorv
Mea culpa. Thought you were trying to avoid returning all your domain objects.
seth
+1  A: 

Couldn't you use find without a where clause: http://grails.org/doc/1.1.1/ref/Domain%20Classes/find.html

e.g. DomainClass.find('from DomainClass')

leebutts
You could expand this pretty easily in a plugin/bootstrap by adding a zero arg find() to each domain classes metaclass that simply calls your method
Ted Naleid