My Grails experience is getting better
My first go around with Grails didn’t go too well, but it has improved I’m glad to say. My simple two object app is moving right along, but I did have one issue that I still haven’t solved.
How to do this?
OK, so what I wanted to do was list the Users and then list the most recent Report from each user. My first attempt just didn’t quite work out. What I tried to do was through each iteration of the users grab the first report like this:
user.reports?.get(0).fileDate
Well that didn’t work — through an exception –, so I looked through the docs with no luck. Then I thought I would get clever and tried:
user.reports[0]?.fileDate
Another exception. Something about a Hibernate PersistentSet not having a getAt() method. Wonderful. My cleverness went up just a notch:
user.reports?.iterator.next.fileDate
Nope. Nada. Can’t remember what the exception was that time though. Oh yea, I checked the docs again thinking maybe I had missed something. Looked through the mailing list archives but had a hard time with that.
What did work
user.reports?.fileDate[0]
I can’t remember what lead to me to that conclusion but it sure isn’t the obvious answer (to me anyway). To me it reads “for this user, get the reports, then get the fileDate array/list and get the 0 indexed item.” Instead of “for this user, get the reports, get the 0 indexed report and get the fileDate.”.
A few other things
One thing I haven’t found is formatting dates for display (NOT edit). Turns out there is(might be?) a GSP tag for this, but there was no docs or anything on it so I couldn’t try it. What else… Oh yea, I couldn’t find the solution to my previous problem, which lead me to the trial and error that I went through above. It also seems that you can’t have methods on domain objects, as Grails thinks they are properties, unless I’m missing something.
I also had a hard time using both the scaffold methods and wanting to override methods. For example, I wanted to override the save method on my ReportController but kept getting exceptions. Once I commented out the scaffold definition I was go to go. Of course I now have to write all of the methods that Grails would have done for me, but I’m taking it as a learning experience. Perhaps I did something wrong, but I’m not upset about it (yet…).
Don’t miss anything, subscribe!
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.
Comments
[...] Yesterday was a very productive day for me in my Grails app that I’m working on. From custom tags to spring usage, it was a good day indeed. One of the first things I had to do was implement a simple login, and the first item on the tutorials page was exactly that. Besides the login stuff, it also showed me about the interceptors, which is a pretty cool feature. [...]
Hmm, just started Grails today and ran into the same problem.
I did this instead:
def arr = user.reports.toArray()
def first = arr[0]
tada…
via the hibernate api @ http://www.hibernate.org/hib_docs/v3/api/org/hibernate/collection/PersistentSet.html

Run:
grails generate-controller Blah
To create a controller that implements all the CRUD methods.
Oh and make sure you register your blog on http://groovyblogs.org
Cheers!
Graeme