0

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?

Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90
  • 1
    By default grails uses h2 database, on each app. run database dropped and created fresh one. If you had restarted your application previous entry will be lost. – ABC Nov 11 '13 at 14:56

1 Answers1

1

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.

rxn1d
  • 1,236
  • 6
  • 18