I'm using the grails console to play with my relationships. I'm using the excersises on the book Grails in Action
:
I have the relationship:
class User {
...
Profile profile
static hasMany = [posts: Post, tags: Tag, following: User]
...
User.get(3).addToFollowing( User.get(2) ).save()
User.list().each { print it.following }
yields
null null [com.grailsinaction.User : 2] null null
and again running:
User.get(1).addToFollowing( User.get(2) ).save()
User.list().each { print it.following }
gives
[com.grailsinaction.User : 2] null null null null
Looks like the first addToFollowing
is lost... did I forget anything?
Try to use:
User.get(3).addToFollowing( User.get(2) ).save(flush: true)
The object will not be persisted immediately unless the flush argument is used. See related link.