Reputation: 15992
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.
Upvotes: 1
Views: 436
Reputation: 35864
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
Upvotes: 3