Question

Assuming I have a Person and a Status. If Status is like this :

class Status {
    String text
    Person author
}

I would have done something like this to get the messages list of the current user :

def messages = Status.withCriteria {
    author { 
        eq 'username', currentPerson.username
    }
}

But if my relationship in Status is like this, how can I do so?

static belongsTo = [Person]

Thanks for your help.

Was it helpful?

Solution

I tend to use the map notation for belongsTo, so I would do it like this:

class Status {
   String text
   static belongsTo = [author: Person]
}

Then you're query is easy:

def messages = Status.findAllByAuthor(currentPerson)

If you had bidirectional added into Person with hasMany:

class Person {

   static hasMany = [messages: Status]
}

You could also do this:

def messages = currentPerson.messages
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top