These are my domain classes:
class Game {
static hasMany = [players: User]
static belongsTo = [owner: User]
}
class User {
static hasMany = [games: Game]
}
If I try to use them as they are I get No owner defined between domain classes
. So I need to set the owner of the relationship. Adding static belongsTo = Game
to User
causes Domain classes cannot own each other in a many-to-many relationship
.
The only other option I can think of is to add static belongsTo = User
to the Game
class but I already have a belongsTo
there.
How do I model this?
class Game {
User owner
static hasMany = [players: User]
static belongsTo = User
}
class User {
static hasMany = [games: Game]
}
You will have to specify one side of the relationship, as the owner, by doing this you will make User
domain class as the owner of the many to many relationship.
The belongsTo
field controls where the dynamic addTo*()
methods can be used
from. we’re able to call User.addToGames()
because Game
belongsTo
User
. we can’t call Game.addToPlayers()
.
Try this:
class Game {
User owner
static hasMany = [players: User]
static belongsTo = User
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With