views:

192

answers:

2

Hi there, I'm following the code examples in 'The Definitive Guide to Grails' by Graeme Keith Rocher, and have come across a rather unusual stumbling block.

Essentially, 2 domain classes exist - Bookmark & Tag.

Bookmark:

class Bookmark {
static hasMany = [tags:Tag]

URL url
String title
String notes
Date dateCreated = new Date()

}

Tag:

class Tag{
static belongsTo= Bookmark

Bookmark bookmark
String name

}

I'm instructed to launch the Grails Console (is this the same as the groovy console)and create a new object as follows.

def b = new Bookmark(url: new URL('http://grails.org/'), title:'Grails', notes:'Groovy')

This results in:

Result: Bookmark : null

According to the book, GORM automatically provides an implementation of an addTag method. So I code...

b.addTag( new Tag(name: 'grails'))

Only to get whammed with the error message:

Exception thrown: No such property: b for class: ConsoleScript1

groovy.lang.MissingPropertyException: No such property: b for class: ConsoleScript1 at ConsoleScript1.run(ConsoleScript1:2)

The author hasn't accounted for this in the book. I was wondering if anyone could help me out?

Thanks.

+2  A: 

The groovy console is not the same as the grails console. To access the grails console, type grails console in your application directory - you should get a Java GUI app. It's possible that the example will work then because grails add some stuff to the standard Groovy.

Also, your problem isn't the addTag method, but the item b that you defined which cannot be found. Try entering the whole script into the console at once and executing it, instead of executing it line by line.

Gregor Petrin
Thanks for the clarification, however even when I enter in 'grails console' in CMD , the Java GUI app that launches is titled 'Groovy Console'.
Lycana
Ah, you're right.. still, it does behave differently than the command line console, I recommend the GUI version as it is more consistent with how the appliaction itself works.I was just thinking - I don't have the book and I don't know what version of Grails you are using - but shouldn't the syntax be `b.addToTags(..)`? That's what the auto method is called in Grails 1.1.1.
Gregor Petrin
Thanks for the recommendation.With regards to the b.addtoTags(..) that is present in Grails, but in the book, it says addTag(..)I decided to reduce everything to the bare minimum and its still failing.Bookmark Class:class Bookmark {String titleString notes}...and in GroovyConsole, I type:def b = new Bookmark(title:'Grails', notes:'Groovy')which still results in:Result: Bookmark : nullWould you know why it is null?
Lycana
Don't know for sure, I turned off my work machine already so this is a bit of guesswork.. but maybe because the toString() method is not defined? Try defining it, e.g. something like `String toString() {return 'title}`
Gregor Petrin
..and that ' is of course too much - is there a way here to edit your own comments?
Gregor Petrin
+2  A: 

Are you reading the 1st edition of the book? If so it's quite outdated. The add* methods have been deprecated since 0.5. It was replaced by addTo* so do this instead:

b.addToTags( new Tag(name: 'grails'))

Assuming your code example shouldn't have Bookmarks defined twice (copy and paste error?) and Tag might look like this:

class Tag {
    String name
}
mbrevoort